*
* Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at>
*
+ * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
+ * the algorithm used
+ *
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
- * the algorithm used
*/
/**
uint64_t stats[3][256];
uint8_t len[3][256];
uint32_t bits[3][256];
+ uint32_t pix_bgr_map[1<<VLC_BITS];
VLC vlc[6]; //Y,U,V,YY,YU,YV
AVFrame picture;
uint8_t *bitstream_buffer;
len[i] = len[up[i]] + 1;
for(i=0; i<size; i++) {
dst[i] = len[up[i]] + 1;
- if(dst[i] > 32) break;
+ if(dst[i] >= 32) break;
}
if(i==size) break;
}
#endif /* CONFIG_ENCODERS */
static void generate_joint_tables(HYuvContext *s){
- // TODO rgb
+ uint16_t symbols[1<<VLC_BITS];
+ uint16_t bits[1<<VLC_BITS];
+ uint8_t len[1<<VLC_BITS];
if(s->bitstream_bpp < 24){
- uint16_t symbols[1<<VLC_BITS];
- uint16_t bits[1<<VLC_BITS];
- uint8_t len[1<<VLC_BITS];
int p, i, y, u;
for(p=0; p<3; p++){
for(i=y=0; y<256; y++){
free_vlc(&s->vlc[3+p]);
init_vlc_sparse(&s->vlc[3+p], VLC_BITS, i, len, 1, 1, bits, 2, 2, symbols, 2, 2, 0);
}
+ }else{
+ uint8_t (*map)[4] = (uint8_t(*)[4])s->pix_bgr_map;
+ int i, b, g, r, code;
+ int p0 = s->decorrelate;
+ int p1 = !s->decorrelate;
+ // restrict the range to +/-16 becaues that's pretty much guaranteed to
+ // cover all the combinations that fit in 11 bits total, and it doesn't
+ // matter if we miss a few rare codes.
+ for(i=0, g=-16; g<16; g++){
+ int len0 = s->len[p0][g&255];
+ int limit0 = VLC_BITS - len0;
+ if(limit0 < 2)
+ continue;
+ for(b=-16; b<16; b++){
+ int len1 = s->len[p1][b&255];
+ int limit1 = limit0 - len1;
+ if(limit1 < 1)
+ continue;
+ code = (s->bits[p0][g&255] << len1) + s->bits[p1][b&255];
+ for(r=-16; r<16; r++){
+ int len2 = s->len[2][r&255];
+ if(len2 > limit1)
+ continue;
+ len[i] = len0 + len1 + len2;
+ bits[i] = (code << len2) + s->bits[2][r&255];
+ if(s->decorrelate){
+ map[i][G] = g;
+ map[i][B] = g+b;
+ map[i][R] = g+r;
+ }else{
+ map[i][B] = g;
+ map[i][G] = b;
+ map[i][R] = r;
+ }
+ i++;
+ }
+ }
+ }
+ free_vlc(&s->vlc[3]);
+ init_vlc(&s->vlc[3], VLC_BITS, i, len, 1, 1, bits, 2, 2, 0);
}
}
}
#ifdef CONFIG_DECODERS
-static int decode_init(AVCodecContext *avctx)
+static av_cold int decode_init(AVCodecContext *avctx)
{
HYuvContext *s = avctx->priv_data;
return index;
}
-static int encode_init(AVCodecContext *avctx)
+static av_cold int encode_init(AVCodecContext *avctx)
{
HYuvContext *s = avctx->priv_data;
int i, j;
}
#endif /* CONFIG_ENCODERS */
-static void decode_bgr_bitstream(HYuvContext *s, int count){
+static av_always_inline void decode_bgr_1(HYuvContext *s, int count, int decorrelate, int alpha){
int i;
-
- if(s->decorrelate){
- if(s->bitstream_bpp==24){
- for(i=0; i<count; i++){
- s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
- s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G];
- s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G];
- }
+ for(i=0; i<count; i++){
+ int code = get_vlc2(&s->gb, s->vlc[3].table, VLC_BITS, 1);
+ if(code != -1){
+ *(uint32_t*)&s->temp[0][4*i] = s->pix_bgr_map[code];
+ }else if(decorrelate){
+ s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
+ s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G];
+ s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G];
}else{
- for(i=0; i<count; i++){
- s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
- s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G];
- s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G];
- get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?!
- }
+ s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
+ s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
+ s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
}
+ if(alpha)
+ get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?!
+ }
+}
+
+static void decode_bgr_bitstream(HYuvContext *s, int count){
+ if(s->decorrelate){
+ if(s->bitstream_bpp==24)
+ decode_bgr_1(s, count, 1, 0);
+ else
+ decode_bgr_1(s, count, 1, 1);
}else{
- if(s->bitstream_bpp==24){
- for(i=0; i<count; i++){
- s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
- s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
- s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
- }
- }else{
- for(i=0; i<count; i++){
- s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
- s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
- s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
- get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?!
- }
- }
+ if(s->bitstream_bpp==24)
+ decode_bgr_1(s, count, 0, 0);
+ else
+ decode_bgr_1(s, count, 0, 1);
}
}
s->last_slice_end= y + h;
}
-static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
+static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
HYuvContext *s = avctx->priv_data;
const int width= s->width;
const int width2= s->width>>1;
s->bitstream_buffer= av_fast_realloc(s->bitstream_buffer, &s->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
- s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4);
+ s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
if(p->data[0])
avctx->release_buffer(avctx, p);
decode_bgr_bitstream(s, width-1);
add_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb);
- for(y=s->height-2; y>=0; y--){ //yes its stored upside down
+ for(y=s->height-2; y>=0; y--){ //Yes it is stored upside down.
decode_bgr_bitstream(s, width);
add_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb);
}
#ifdef CONFIG_DECODERS
-static int decode_end(AVCodecContext *avctx)
+static av_cold int decode_end(AVCodecContext *avctx)
{
HYuvContext *s = avctx->priv_data;
int i;
common_end(s);
av_freep(&s->bitstream_buffer);
- for(i=0; i<3; i++){
+ for(i=0; i<6; i++){
free_vlc(&s->vlc[i]);
}
return size*4;
}
-static int encode_end(AVCodecContext *avctx)
+static av_cold int encode_end(AVCodecContext *avctx)
{
HYuvContext *s = avctx->priv_data;
decode_end,
decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
- NULL
+ NULL,
+ .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
};
AVCodec ffvhuff_decoder = {
decode_end,
decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
- NULL
+ NULL,
+ .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
};
#endif
encode_init,
encode_frame,
encode_end,
- .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_RGB32, -1},
+ .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
+ .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
};
AVCodec ffvhuff_encoder = {
encode_init,
encode_frame,
encode_end,
- .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB32, -1},
+ .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
+ .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
};
#endif //CONFIG_ENCODERS