Revision 81ada1ce libavcodec/mjpeg.c

View differences:

libavcodec/mjpeg.c
470 470
    put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
471 471
}
472 472

  
473
static void escape_FF(MpegEncContext *s)
474
{
475
    int size= get_bit_count(&s->pb) - s->header_bits;
476
    int i, ff_count;
477
    uint8_t *buf= s->pb.buf + (s->header_bits>>3);
478
    int align= (-(int)(buf))&3;
479
    
480
    assert((size&7) == 0);
481
    size >>= 3;
482
    
483
    ff_count=0;
484
    for(i=0; i<size && i<align; i++){
485
        if(buf[i]==0xFF) ff_count++;
486
    }
487
    for(; i<size-15; i+=16){
488
        int acc, v;
489

  
490
        v= *(uint32_t*)(&buf[i]);
491
        acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
492
        v= *(uint32_t*)(&buf[i+4]);
493
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
494
        v= *(uint32_t*)(&buf[i+8]);
495
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
496
        v= *(uint32_t*)(&buf[i+12]);
497
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
498

  
499
        acc>>=4;
500
        acc+= (acc>>16);
501
        acc+= (acc>>8);
502
        ff_count+= acc&0xFF;
503
    }
504
    for(; i<size; i++){
505
        if(buf[i]==0xFF) ff_count++;
506
    }
507

  
508
    if(ff_count==0) return;
509
    
510
    /* skip put bits */
511
    for(i=0; i<ff_count-3; i+=4)
512
        put_bits(&s->pb, 32, 0);
513
    put_bits(&s->pb, (ff_count-i)*8, 0);
514
    flush_put_bits(&s->pb); 
515

  
516
    for(i=size-1; ff_count; i--){
517
        int v= buf[i];
518

  
519
        if(v==0xFF){
520
//printf("%d %d\n", i, ff_count);
521
            buf[i+ff_count]= 0;
522
            ff_count--;
523
        }
524

  
525
        buf[i+ff_count]= v;
526
    }
527
}
528

  
473 529
void mjpeg_picture_trailer(MpegEncContext *s)
474 530
{
475
    jflush_put_bits(&s->pb);
531
    int pad= (-get_bit_count(&s->pb))&7;
532
    
533
    put_bits(&s->pb, pad,0xFF>>(8-pad));
534
    flush_put_bits(&s->pb);
535

  
536
    escape_FF(s);
537

  
476 538
    put_marker(&s->pb, EOI);
477 539
}
478 540

  
......
482 544
    int mant, nbits;
483 545

  
484 546
    if (val == 0) {
485
        jput_bits(&s->pb, huff_size[0], huff_code[0]);
547
        put_bits(&s->pb, huff_size[0], huff_code[0]);
486 548
    } else {
487 549
        mant = val;
488 550
        if (val < 0) {
......
497 559
            nbits++;
498 560
        }
499 561
            
500
        jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
562
        put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
501 563
        
502
        jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
564
        put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
503 565
    }
504 566
}
505 567

  
......
537 599
            run++;
538 600
        } else {
539 601
            while (run >= 16) {
540
                jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
602
                put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
541 603
                run -= 16;
542 604
            }
543 605
            mant = val;
......
554 616
            }
555 617
            code = (run << 4) | nbits;
556 618

  
557
            jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
619
            put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
558 620
        
559
            jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
621
            put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
560 622
            run = 0;
561 623
        }
562 624
    }
563 625

  
564 626
    /* output EOB only if not already 64 values */
565 627
    if (last_index < 63 || run != 0)
566
        jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
628
        put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
567 629
}
568 630

  
569 631
void mjpeg_encode_mb(MpegEncContext *s, 
......
1316 1378
			*(dst++) = x;
1317 1379
			if (x == 0xff)
1318 1380
			{
1381
			    while(*src == 0xff) src++;
1382

  
1319 1383
			    x = *(src++);
1320 1384
			    if (x >= 0xd0 && x <= 0xd7)
1321 1385
				*(dst++) = x;

Also available in: Unified diff