📄 huffyuv.c
字号:
default: s->predictor= LEFT; //OLD s->decorrelate= 0; break; } s->bitstream_bpp= avctx->bits_per_sample & ~7; if(read_old_huffman_tables(s) < 0) return -1; } s->interlaced= height > 288; c_size= 0; switch(s->bitstream_bpp){ case 12: avctx->pix_fmt = PIX_FMT_YUV420P; stride= (width+15)&~15; c_size= height*stride/4; break; case 16: if(s->yuy2){ avctx->pix_fmt = PIX_FMT_YUV422; stride= (width*2+15)&~15; }else{ avctx->pix_fmt = PIX_FMT_YUV422P; stride= (width+15)&~15; c_size= height*stride/2; } break; case 24: case 32: if(s->bgr32){ avctx->pix_fmt = PIX_FMT_BGRA32; stride= (width*4+15)&~15; }else{ avctx->pix_fmt = PIX_FMT_BGR24; stride= (width*3+15)&~15; } break; default: assert(0); stride=0; //gcc fix } y_size= height*stride; if(!(avctx->flags&CODEC_FLAG_DR1)){ s->linesize[0]= stride; s->picture[0]= av_mallocz(y_size); if(c_size){ s->picture[1]= av_mallocz(c_size); s->picture[2]= av_mallocz(c_size); s->linesize[1]= s->linesize[2]= stride/2; memset(s->picture[1], 128, c_size); memset(s->picture[2], 128, c_size); } } // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); return 0;}static void store_table(HYuvContext *s, uint8_t *len){ int i; int index= s->avctx->extradata_size; for(i=0; i<256;){ int cur=i; int val= len[i]; int repeat; for(; i<256 && len[i]==val; i++); repeat= i - cur; if(repeat>7){ ((uint8_t*)s->avctx->extradata)[index++]= val; ((uint8_t*)s->avctx->extradata)[index++]= repeat; }else{ ((uint8_t*)s->avctx->extradata)[index++]= val | (repeat<<5); } } s->avctx->extradata_size= index;}static int encode_init(AVCodecContext *avctx){ HYuvContext *s = avctx->priv_data; int i, j, width, height; s->avctx= avctx; s->flags= avctx->flags; dsputil_init(&s->dsp, avctx->dsp_mask); width= s->width= avctx->width; height= s->height= avctx->height; assert(width && height); avctx->extradata= av_mallocz(1024*10); avctx->stats_out= av_mallocz(1024*10); s->version=2; switch(avctx->pix_fmt){ case PIX_FMT_YUV420P: if(avctx->strict_std_compliance>=0){ fprintf(stderr, "YV12-huffyuv is experimental, there WILL be no compatbility! (use (v)strict=-1)\n"); return -1; } s->bitstream_bpp= 12; break; case PIX_FMT_YUV422P: s->bitstream_bpp= 16; break; default: fprintf(stderr, "format not supported\n"); return -1; } avctx->bits_per_sample= s->bitstream_bpp; s->decorrelate= s->bitstream_bpp >= 24; s->predictor= avctx->prediction_method; ((uint8_t*)avctx->extradata)[0]= s->predictor; ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp; ((uint8_t*)avctx->extradata)[2]= ((uint8_t*)avctx->extradata)[3]= 0; s->avctx->extradata_size= 4; if(avctx->stats_in){ char *p= avctx->stats_in; for(i=0; i<3; i++) for(j=0; j<256; j++) s->stats[i][j]= 1; for(;;){ for(i=0; i<3; i++){ char *next; for(j=0; j<256; j++){ s->stats[i][j]+= strtol(p, &next, 0); if(next==p) return -1; p=next; } } if(p[0]==0 || p[1]==0 || p[2]==0) break; } }else{ for(i=0; i<3; i++) for(j=0; j<256; j++){ int d= FFMIN(j, 256-j); s->stats[i][j]= 100000000/(d+1); } } for(i=0; i<3; i++){ generate_len_table(s->len[i], s->stats[i], 256); if(generate_bits_table(s->bits[i], s->len[i])<0){ return -1; } store_table(s, s->len[i]); } for(i=0; i<3; i++) for(j=0; j<256; j++) s->stats[i][j]= 0; s->interlaced= height > 288; // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); s->picture_number=0; return 0;}static void decode_422_bitstream(HYuvContext *s, int count){ int i; count/=2; for(i=0; i<count; i++){ s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); s->temp[1][ i ]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); s->temp[2][ i ]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); }}static void decode_gray_bitstream(HYuvContext *s, int count){ int i; count/=2; for(i=0; i<count; i++){ s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); }}static void encode_422_bitstream(HYuvContext *s, int count){ int i; count/=2; if(s->flags&CODEC_FLAG_PASS1){ for(i=0; i<count; i++){ s->stats[0][ s->temp[0][2*i ] ]++; s->stats[1][ s->temp[1][ i ] ]++; s->stats[0][ s->temp[0][2*i+1] ]++; s->stats[2][ s->temp[2][ i ] ]++; } }else{ for(i=0; i<count; i++){ put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); put_bits(&s->pb, s->len[1][ s->temp[1][ i ] ], s->bits[1][ s->temp[1][ i ] ]); put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]); } }}static void encode_gray_bitstream(HYuvContext *s, int count){ int i; count/=2; if(s->flags&CODEC_FLAG_PASS1){ for(i=0; i<count; i++){ s->stats[0][ s->temp[0][2*i ] ]++; s->stats[0][ s->temp[0][2*i+1] ]++; } }else{ for(i=0; i<count; i++){ put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); } }}static void decode_bgr_bitstream(HYuvContext *s, int count){ int i; if(s->decorrelate){ if(s->bitstream_bpp==24){ for(i=0; i<count; i++){ s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1]; s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1]; } }else{ for(i=0; i<count; i++){ s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1]; s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1]; get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! } } }else{ if(s->bitstream_bpp==24){ for(i=0; i<count; i++){ s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); } }else{ for(i=0; i<count; i++){ s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! } } }}static void draw_slice(HYuvContext *s, int y){ int h, cy; UINT8 *src_ptr[3]; if(s->avctx->draw_horiz_band==NULL) return; h= y - s->last_slice_end; y -= h; if(s->bitstream_bpp==12){ cy= y>>1; }else{ cy= y; } src_ptr[0] = s->picture[0] + s->linesize[0]*y; src_ptr[1] = s->picture[1] + s->linesize[1]*cy; src_ptr[2] = s->picture[2] + s->linesize[2]*cy; emms_c(); s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize[0], y, s->width, h); s->last_slice_end= y + h;}static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ HYuvContext *s = avctx->priv_data; const int width= s->width; const int width2= s->width>>1; const int height= s->height; int fake_ystride, fake_ustride, fake_vstride; int i; AVPicture *picture = data; *data_size = 0; /* no supplementary picture */ if (buf_size == 0) return 0; bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4); init_get_bits(&s->gb, s->bitstream_buffer, buf_size); if(avctx->flags&CODEC_FLAG_DR1){ if(avctx->get_buffer_callback(avctx, s->width, s->height, I_TYPE) < 0){ fprintf(stderr, "get_buffer() failed\n"); return -1; } s->linesize[0]= avctx->dr_stride; s->linesize[1]= s->linesize[2]= avctx->dr_uvstride; for(i=0; i<3;i++) s->picture[i]= avctx->dr_buffer[i]; } fake_ystride= s->interlaced ? s->linesize[0]*2 : s->linesize[0]; fake_ustride= s->interlaced ? s->linesize[1]*2 : s->linesize[1]; fake_vstride= s->interlaced ? s->linesize[2]*2 : s->linesize[2]; s->last_slice_end= 0; if(s->bitstream_bpp<24){ int y, cy; int lefty, leftu, leftv; int lefttopy, lefttopu, lefttopv; if(s->yuy2){ s->picture[0][3]= get_bits(&s->gb, 8); s->picture[0][2]= get_bits(&s->gb, 8); s->picture[0][1]= get_bits(&s->gb, 8); s->picture[0][0]= get_bits(&s->gb, 8); fprintf(stderr, "YUY2 output isnt implemenetd yet\n"); return -1; }else{ leftv= s->picture[2][0]= get_bits(&s->gb, 8); lefty= s->picture[0][1]= get_bits(&s->gb, 8); leftu= s->picture[1][0]= get_bits(&s->gb, 8); s->picture[0][0]= get_bits(&s->gb, 8); switch(s->predictor){ case LEFT: case PLANE: decode_422_bitstream(s, width-2); lefty= add_left_prediction(s->picture[0] + 2, s->temp[0], width-2, lefty); if(!(s->flags&CODEC_FLAG_GRAY)){ leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu); leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -