Revision 11087086 libavcodec/huffyuv.c

View differences:

libavcodec/huffyuv.c
61 61
    int bgr32;                              //use bgr32 instead of bgr24
62 62
    int width, height;
63 63
    int flags;
64
    int context;
64 65
    int picture_number;
65 66
    int last_slice_end;
66 67
    uint8_t __align8 temp[3][2560];
......
306 307
printf("%6X, %2d,  %3d\n", s->bits[i][j], s->len[i][j], j);
307 308
}
308 309
#endif
310
        free_vlc(&s->vlc[i]);
309 311
        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
310 312
    }
311 313
    
312
    return 0;
314
    return (get_bits_count(&gb)+7)/8;
313 315
}
314 316

  
315 317
static int read_old_huffman_tables(HYuvContext *s){
......
332 334
    memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t));
333 335
    memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t));
334 336
    
335
    for(i=0; i<3; i++)
337
    for(i=0; i<3; i++){
338
        free_vlc(&s->vlc[i]);
336 339
        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
340
    }
337 341
    
338 342
    return 0;
339 343
#else
......
351 355
    s->flags= avctx->flags;
352 356
        
353 357
    dsputil_init(&s->dsp, avctx);
358
    memset(s->vlc, 0, 3*sizeof(VLC));
354 359
    
355 360
    width= s->width= avctx->width;
356 361
    height= s->height= avctx->height;
......
377 382
        s->bitstream_bpp= ((uint8_t*)avctx->extradata)[1];
378 383
        if(s->bitstream_bpp==0) 
379 384
            s->bitstream_bpp= avctx->bits_per_sample&~7;
385
        s->context= ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0;
380 386
            
381 387
        if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0)
382 388
            return -1;
......
404 410
            break;
405 411
        }
406 412
        s->bitstream_bpp= avctx->bits_per_sample & ~7;
413
        s->context= 0;
407 414
        
408 415
        if(read_old_huffman_tables(s) < 0)
409 416
            return -1;
......
442 449
    return 0;
443 450
}
444 451

  
445
static void store_table(HYuvContext *s, uint8_t *len){
452
static int store_table(HYuvContext *s, uint8_t *len, uint8_t *buf){
446 453
    int i;
447
    int index= s->avctx->extradata_size;
454
    int index= 0;
448 455

  
449 456
    for(i=0; i<256;){
450 457
        int val= len[i];
......
455 462
        
456 463
        assert(val < 32 && val >0 && repeat<256 && repeat>0);
457 464
        if(repeat>7){
458
            ((uint8_t*)s->avctx->extradata)[index++]= val;
459
            ((uint8_t*)s->avctx->extradata)[index++]= repeat;
465
            buf[index++]= val;
466
            buf[index++]= repeat;
460 467
        }else{
461
            ((uint8_t*)s->avctx->extradata)[index++]= val | (repeat<<5);
468
            buf[index++]= val | (repeat<<5);
462 469
        }
463 470
    }
464 471
    
465
    s->avctx->extradata_size= index;
472
    return index;
466 473
}
467 474

  
468 475
static int encode_init(AVCodecContext *avctx)
......
508 515
    if(s->interlaced != ( height > 288 )){
509 516
        av_log(avctx, AV_LOG_INFO, "using huffyuv 2.2.0 or newer interlacing flag\n");
510 517
    }
518
    if(avctx->context_model==1){
519
        s->context= avctx->context_model;
520
        if(avctx->strict_std_compliance>=0){
521
            av_log(avctx, AV_LOG_ERROR, "Warning: per-frame huffman tables are not supported by windows huffyuv; use context=0 or use (v)strict=-1\n");
522
            return -1;
523
        }
524
        if(s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
525
            av_log(avctx, AV_LOG_ERROR, "context=1 is not compatible with 2 pass huffyuv encoding\n");
526
            return -1;
527
        }
528
        av_log(avctx, AV_LOG_INFO, "using per-frame huffman tables\n");
529
    }else s->context= 0;
511 530
    
512 531
    ((uint8_t*)avctx->extradata)[0]= s->predictor;
513 532
    ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp;
514 533
    ((uint8_t*)avctx->extradata)[2]= 0x20 | (s->interlaced ? 0x10 : 0);
534
    if(s->context)
535
        ((uint8_t*)avctx->extradata)[2]|= 0x40;
515 536
    ((uint8_t*)avctx->extradata)[3]= 0;
516 537
    s->avctx->extradata_size= 4;
517 538
    
......
550 571
            return -1;
551 572
        }
552 573
        
553
        store_table(s, s->len[i]);
574
        s->avctx->extradata_size+=
575
        store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
554 576
    }
555 577

  
556
    for(i=0; i<3; i++)
557
        for(j=0; j<256; j++)
558
            s->stats[i][j]= 0;
578
    if(s->context){
579
        for(i=0; i<3; i++){
580
            int pels = width*height / (i?40:10);
581
            for(j=0; j<256; j++){
582
                int d= FFMIN(j, 256-j);
583
                s->stats[i][j]= pels/(d+1);
584
            }
585
        }
586
    }else{
587
        for(i=0; i<3; i++)
588
            for(j=0; j<256; j++)
589
                s->stats[i][j]= 0;
590
    }
559 591
    
560 592
//    printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced);
561 593

  
......
599 631
            s->stats[0][ s->temp[0][2*i+1] ]++;
600 632
            s->stats[2][ s->temp[2][  i  ] ]++;
601 633
        }
634
    }else if(s->context){
635
        for(i=0; i<count; i++){
636
            s->stats[0][ s->temp[0][2*i  ] ]++;
637
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i  ] ], s->bits[0][ s->temp[0][2*i  ] ]);
638
            s->stats[1][ s->temp[1][  i  ] ]++;
639
            put_bits(&s->pb, s->len[1][ s->temp[1][  i  ] ], s->bits[1][ s->temp[1][  i  ] ]);
640
            s->stats[0][ s->temp[0][2*i+1] ]++;
641
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]);
642
            s->stats[2][ s->temp[2][  i  ] ]++;
643
            put_bits(&s->pb, s->len[2][ s->temp[2][  i  ] ], s->bits[2][ s->temp[2][  i  ] ]);
644
        }
602 645
    }else{
603 646
        for(i=0; i<count; i++){
604 647
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i  ] ], s->bits[0][ s->temp[0][2*i  ] ]);
......
618 661
            s->stats[0][ s->temp[0][2*i  ] ]++;
619 662
            s->stats[0][ s->temp[0][2*i+1] ]++;
620 663
        }
664
    }else if(s->context){
665
        for(i=0; i<count; i++){
666
            s->stats[0][ s->temp[0][2*i  ] ]++;
667
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i  ] ], s->bits[0][ s->temp[0][2*i  ] ]);
668
            s->stats[0][ s->temp[0][2*i+1] ]++;
669
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]);
670
        }
621 671
    }else{
622 672
        for(i=0; i<count; i++){
623 673
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i  ] ], s->bits[0][ s->temp[0][2*i  ] ]);
......
696 746
    const int height= s->height;
697 747
    int fake_ystride, fake_ustride, fake_vstride;
698 748
    AVFrame * const p= &s->picture;
749
    int table_size= 0;
699 750

  
700 751
    AVFrame *picture = data;
701 752

  
......
705 756

  
706 757
    s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4);
707 758
    
708
    init_get_bits(&s->gb, s->bitstream_buffer, buf_size*8);
709

  
710 759
    if(p->data[0])
711 760
        avctx->release_buffer(avctx, p);
712 761

  
......
715 764
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
716 765
        return -1;
717 766
    }
767
    
768
    if(s->context){
769
        table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
770
        if(table_size < 0)
771
            return -1;
772
    }
773

  
774
    init_get_bits(&s->gb, s->bitstream_buffer+table_size, (buf_size-table_size)*8);
718 775

  
719 776
    fake_ystride= s->interlaced ? p->linesize[0]*2  : p->linesize[0];
720 777
    fake_ustride= s->interlaced ? p->linesize[1]*2  : p->linesize[1];
......
943 1000
    const int fake_ustride= s->interlaced ? pict->linesize[1]*2  : pict->linesize[1];
944 1001
    const int fake_vstride= s->interlaced ? pict->linesize[2]*2  : pict->linesize[2];
945 1002
    AVFrame * const p= &s->picture;
946
    int i, size;
1003
    int i, j, size=0;
947 1004

  
948
    init_put_bits(&s->pb, buf, buf_size);
949
    
950 1005
    *p = *pict;
951 1006
    p->pict_type= FF_I_TYPE;
952 1007
    p->key_frame= 1;
953 1008
    
1009
    if(s->context){
1010
        for(i=0; i<3; i++){
1011
            generate_len_table(s->len[i], s->stats[i], 256);
1012
            if(generate_bits_table(s->bits[i], s->len[i])<0)
1013
                return -1;
1014
            size+= store_table(s, s->len[i], &buf[size]);
1015
        }
1016

  
1017
        for(i=0; i<3; i++)
1018
            for(j=0; j<256; j++)
1019
                s->stats[i][j] >>= 1;
1020
    }
1021

  
1022
    init_put_bits(&s->pb, buf+size, buf_size-size);
1023

  
954 1024
    if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
955 1025
        int lefty, leftu, leftv, y, cy;
956 1026

  
......
1060 1130
    }
1061 1131
    emms_c();
1062 1132
    
1063
    size= (put_bits_count(&s->pb)+31)/32;
1133
    size+= (put_bits_count(&s->pb)+31)/8;
1134
    size/= 4;
1064 1135
    
1065 1136
    if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){
1066 1137
        int j;

Also available in: Unified diff