Revision 81ada1ce

View differences:

libavcodec/common.c
87 87
#endif
88 88
}
89 89

  
90
/* pad the end of the output stream with zeros */
91
#ifndef ALT_BITSTREAM_WRITER
92
void jflush_put_bits(PutBitContext *s)
93
{
94
    unsigned int b;
95
    s->bit_buf<<= s->bit_left;
96
    s->bit_buf |= ~1U >> (32 - s->bit_left); /* set all the unused bits to one */
97

  
98
    while (s->bit_left < 32) {
99
        b = s->bit_buf >> 24;
100
        *s->buf_ptr++ = b;
101
        if (b == 0xff)
102
            *s->buf_ptr++ = 0;
103
        s->bit_buf<<=8;
104
        s->bit_left+=8;
105
    }
106
    s->bit_left=32;
107
    s->bit_buf=0;
108
}
109
#else
110
void jflush_put_bits(PutBitContext *s)
111
{
112
    int num= (  - s->index) & 7;
113
    jput_bits(s, num,0xFF>>(8-num));
114
}
115
#endif
116

  
117 90
void put_string(PutBitContext * pbc, char *s)
118 91
{
119 92
    while(*s){
libavcodec/common.h
108 108
typedef signed long long INT64;
109 109

  
110 110
#    ifdef HAVE_AV_CONFIG_H
111

  
112 111
#        ifndef INT64_C
113 112
#            define INT64_C(c)     (c ## LL)
114 113
#            define UINT64_C(c)    (c ## ULL)
......
117 116
#        ifdef USE_FASTMEMCPY
118 117
#            include "fastmemcpy.h"
119 118
#        endif
120

  
121 119
#    endif /* HAVE_AV_CONFIG_H */
122 120

  
123 121
#endif /* !CONFIG_WIN32 */
......
220 218
void flush_put_bits(PutBitContext *s);
221 219
void put_string(PutBitContext * pbc, char *s);
222 220

  
223
/* jpeg specific put_bits */
224
void jflush_put_bits(PutBitContext *s);
225

  
226 221
/* bit input */
227 222

  
228 223
typedef struct GetBitContext {
......
383 378
}
384 379
#endif
385 380

  
386
#ifndef ALT_BITSTREAM_WRITER
387
/* for jpeg : escape 0xff with 0x00 after it */
388
static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
389
{
390
    unsigned int bit_buf, b;
391
    int bit_left, i;
392
    
393
    assert(n == 32 || value < (1U << n));
394

  
395
    bit_buf = s->bit_buf;
396
    bit_left = s->bit_left;
397

  
398
    //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
399
    /* XXX: optimize */
400
    if (n < bit_left) {
401
        bit_buf = (bit_buf<<n) | value;
402
        bit_left-=n;
403
    } else {
404
	bit_buf<<=bit_left;
405
        bit_buf |= value >> (n - bit_left);
406
        /* handle escape */
407
        for(i=0;i<4;i++) {
408
            b = (bit_buf >> 24);
409
            *(s->buf_ptr++) = b;
410
            if (b == 0xff)
411
                *(s->buf_ptr++) = 0;
412
            bit_buf <<= 8;
413
        }
414

  
415
	bit_left+= 32 - n;
416
        bit_buf = value;
417
    }
418
    
419
    s->bit_buf = bit_buf;
420
    s->bit_left = bit_left;
421
}
422
#endif
423

  
424

  
425
#ifdef ALT_BITSTREAM_WRITER
426
static inline void jput_bits(PutBitContext *s, int n, int value)
427
{
428
    int index= s->index;
429
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
430
    int v= ptr[0];
431
//if(n>24) printf("%d %d\n", n, value);
432
    
433
    v |= be2me_32(value<<(32-n-(index&7) ));
434
    if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
435
    {
436
	/* handle idiotic (m)jpeg escapes */
437
	uint8_t *bPtr= (uint8_t*)ptr;
438
	int numChecked= ((index+n)>>3) - (index>>3);
439
	
440
	v= be2me_32(v);
441

  
442
	*(bPtr++)= v>>24;
443
	if((v&0xFF000000)==0xFF000000 && numChecked>0){
444
		*(bPtr++)= 0x00;
445
		index+=8;
446
	}
447
	*(bPtr++)= (v>>16)&0xFF;
448
	if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
449
		*(bPtr++)= 0x00;
450
		index+=8;
451
	}
452
	*(bPtr++)= (v>>8)&0xFF;
453
	if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
454
		*(bPtr++)= 0x00;
455
		index+=8;
456
	}
457
	*(bPtr++)= v&0xFF;
458
	if((v&0x000000FF)==0x000000FF && numChecked>3){
459
		*(bPtr++)= 0x00;
460
		index+=8;
461
	}
462
	*((uint32_t*)bPtr)= 0;
463
    }
464
    else
465
    {
466
	ptr[0] = v;
467
	ptr[1] = 0;
468
    }
469

  
470
    index+= n;
471
    s->index= index;
472
 }
473
#endif
474 381

  
475 382
static inline uint8_t* pbBufPtr(PutBitContext *s)
476 383
{
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