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 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
34 static inline int sub_left_prediction(HYuvContext
*s
, uint8_t *dst
,
35 uint8_t *src
, int w
, int left
)
39 for (i
= 0; i
< w
; i
++) {
40 const int temp
= src
[i
];
46 for (i
= 0; i
< 16; i
++) {
47 const int temp
= src
[i
];
51 s
->dsp
.diff_bytes(dst
+ 16, src
+ 16, src
+ 15, w
- 16);
56 static inline void sub_left_prediction_bgr32(HYuvContext
*s
, uint8_t *dst
,
58 int *red
, int *green
, int *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
;
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
)
90 for (i
= 0; i
< 256;) {
94 for (; i
< 256 && len
[i
] == val
&& repeat
< 255; i
++)
97 assert(val
< 32 && val
>0 && repeat
<256 && repeat
>0);
100 buf
[index
++] = repeat
;
102 buf
[index
++] = val
| (repeat
<< 5);
109 static av_cold
int encode_init(AVCodecContext
*avctx
)
111 HYuvContext
*s
= avctx
->priv_data
;
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
120 avctx
->coded_frame
= &s
->picture
;
122 switch (avctx
->pix_fmt
) {
123 case AV_PIX_FMT_YUV420P
:
124 s
->bitstream_bpp
= 12;
126 case AV_PIX_FMT_YUV422P
:
127 s
->bitstream_bpp
= 16;
129 case AV_PIX_FMT_RGB32
:
130 s
->bitstream_bpp
= 24;
133 av_log(avctx
, AV_LOG_ERROR
, "format not supported\n");
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");
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");
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");
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");
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;
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
++)
190 for (i
= 0; i
< 3; i
++) {
193 for (j
= 0; j
< 256; j
++) {
194 s
->stats
[i
][j
] += strtol(p
, &next
, 0);
195 if (next
== p
) return -1;
199 if (p
[0] == 0 || p
[1] == 0 || p
[2] == 0) break;
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) {
217 s
->avctx
->extradata_size
+=
218 store_table(s
, s
->len
[i
], &((uint8_t*)s
->avctx
->extradata
)[s
->avctx
->extradata_size
]);
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);
230 for (i
= 0; i
< 3; i
++)
231 for (j
= 0; j
< 256; j
++)
235 ff_huffyuv_alloc_temp(s
);
241 static int encode_422_bitstream(HYuvContext
*s
, int offset
, int count
)
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");
255 int y1 = y[2 * i + 1];\
261 if (s
->flags
& CODEC_FLAG_PASS1
) {
262 for(i
= 0; i
< count
; i
++) {
270 if (s
->avctx
->flags2
& CODEC_FLAG2_NO_OUTPUT
)
273 for (i
= 0; i
< count
; i
++) {
276 put_bits(&s
->pb
, s
->len
[0][y0
], s
->bits
[0][y0
]);
278 put_bits(&s
->pb
, s
->len
[1][u0
], s
->bits
[1][u0
]);
280 put_bits(&s
->pb
, s
->len
[0][y1
], s
->bits
[0][y1
]);
282 put_bits(&s
->pb
, s
->len
[2][v0
], s
->bits
[2][v0
]);
285 for(i
= 0; i
< count
; i
++) {
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
]);
296 static int encode_gray_bitstream(HYuvContext
*s
, int count
)
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");
306 int y0 = s->temp[0][2 * i];\
307 int y1 = s->temp[0][2 * i + 1];
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]);
317 if (s
->flags
& CODEC_FLAG_PASS1
) {
318 for (i
= 0; i
< count
; i
++) {
323 if (s
->avctx
->flags2
& CODEC_FLAG2_NO_OUTPUT
)
327 for (i
= 0; i
< count
; i
++) {
333 for (i
= 0; i
< count
; i
++) {
341 static int encode_bgr_bitstream(HYuvContext
*s
, int count
)
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");
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;
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
++) {
369 } else if (s
->context
|| (s
->flags
& CODEC_FLAG_PASS1
)) {
370 for (i
= 0; i
< count
; i
++) {
376 for (i
= 0; i
< count
; i
++) {
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
;
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");
404 p
->pict_type
= AV_PICTURE_TYPE_I
;
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)
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
;
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
);
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);
464 for (; y
< height
; y
++,cy
++) {
465 uint8_t *ydst
, *udst
, *vdst
;
467 if (s
->bitstream_bpp
== 12) {
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
);
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
);
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
);
499 lefty
= sub_left_prediction(s
, s
->temp
[0], ydst
, width
, lefty
);
501 encode_gray_bitstream(s
, width
);
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
);
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
;
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
);
548 sub_left_prediction_bgr32(s
, s
->temp
[0], dst
, width
, &leftr
, &leftg
, &leftb
);
550 encode_bgr_bitstream(s
, width
);
553 av_log(avctx
, AV_LOG_ERROR
, "Format not supported!\n");
557 size
+= (put_bits_count(&s
->pb
) + 31) / 8;
558 put_bits(&s
->pb
, 16, 0);
559 put_bits(&s
->pb
, 15, 0);
562 if ((s
->flags
&CODEC_FLAG_PASS1
) && (s
->picture_number
& 31) == 0) {
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
]);
572 snprintf(p
, end
-p
, "\n");
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
);
584 pkt
->size
= size
* 4;
585 pkt
->flags
|= AV_PKT_FLAG_KEY
;
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
);
603 #if CONFIG_HUFFYUV_ENCODER
604 AVCodec ff_huffyuv_encoder
= {
606 .type
= AVMEDIA_TYPE_VIDEO
,
607 .id
= AV_CODEC_ID_HUFFYUV
,
608 .priv_data_size
= sizeof(HYuvContext
),
610 .encode2
= encode_frame
,
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"),
619 #if CONFIG_FFVHUFF_ENCODER
620 AVCodec ff_ffvhuff_encoder
= {
622 .type
= AVMEDIA_TYPE_VIDEO
,
623 .id
= AV_CODEC_ID_FFVHUFF
,
624 .priv_data_size
= sizeof(HYuvContext
),
626 .encode2
= encode_frame
,
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"),