~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/4xm.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * 4XM codec
3
3
 * Copyright (c) 2003 Michael Niedermayer
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 * License along with FFmpeg; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
20
 */
19
 
 
 
21
 
20
22
/**
21
23
 * @file 4xm.c
22
24
 * 4XM codec.
23
25
 */
24
 
 
 
26
 
25
27
#include "avcodec.h"
26
28
#include "dsputil.h"
27
29
#include "mpegvideo.h"
104
106
 
105
107
 
106
108
typedef struct CFrameBuffer{
107
 
    int allocated_size;
108
 
    int size;
 
109
    unsigned int allocated_size;
 
110
    unsigned int size;
109
111
    int id;
110
112
    uint8_t *data;
111
113
}CFrameBuffer;
121
123
    int mv[256];
122
124
    VLC pre_vlc;
123
125
    int last_dc;
124
 
    DCTELEM __align8 block[6][64];
 
126
    DECLARE_ALIGNED_8(DCTELEM, block[6][64]);
125
127
    uint8_t *bitstream_buffer;
126
 
    int bitstream_buffer_size;
 
128
    unsigned int bitstream_buffer_size;
127
129
    CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
128
130
} FourXContext;
129
131
 
141
143
    int z5, z10, z11, z12, z13;
142
144
    int i;
143
145
    int temp[64];
144
 
    
 
146
 
145
147
    for(i=0; i<8; i++){
146
148
        tmp10 = block[8*0 + i] + block[8*4 + i];
147
149
        tmp11 = block[8*0 + i] - block[8*4 + i];
153
155
        tmp3 = tmp10 - tmp13;
154
156
        tmp1 = tmp11 + tmp12;
155
157
        tmp2 = tmp11 - tmp12;
156
 
        
 
158
 
157
159
        z13 = block[8*5 + i] + block[8*3 + i];
158
160
        z10 = block[8*5 + i] - block[8*3 + i];
159
161
        z11 = block[8*1 + i] + block[8*7 + i];
179
181
        temp[8*4 + i] = tmp3 + tmp4;
180
182
        temp[8*3 + i] = tmp3 - tmp4;
181
183
    }
182
 
  
 
184
 
183
185
    for(i=0; i<8*8; i+=8){
184
186
        tmp10 = temp[0 + i] + temp[4 + i];
185
187
        tmp11 = temp[0 + i] - temp[4 + i];
220
222
}
221
223
 
222
224
static void init_vlcs(FourXContext *f){
223
 
    static int done = 0;
224
225
    int i;
225
226
 
226
 
    if (!done) {
227
 
        done = 1;
228
 
 
229
 
        for(i=0; i<4; i++){
230
 
            init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7, 
231
 
                     &block_type_tab[i][0][1], 2, 1,
232
 
                     &block_type_tab[i][0][0], 2, 1);
233
 
        }
 
227
    for(i=0; i<4; i++){
 
228
        init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7,
 
229
                 &block_type_tab[i][0][1], 2, 1,
 
230
                 &block_type_tab[i][0][0], 2, 1, 1);
234
231
    }
235
232
}
236
233
 
287
284
    const int index= size2index[log2h][log2w];
288
285
    const int h= 1<<log2h;
289
286
    int code= get_vlc2(&f->gb, block_type_vlc[index].table, BLOCK_TYPE_VLC_BITS, 1);
290
 
    
 
287
 
291
288
    assert(code>=0 && code<=6);
292
289
 
293
290
    if(code == 0){
328
325
    uint16_t *src= (uint16_t*)f->last_picture.data[0];
329
326
    uint16_t *dst= (uint16_t*)f->current_picture.data[0];
330
327
    const int stride= f->current_picture.linesize[0]>>1;
331
 
    const int bitstream_size= get32(buf+8);
332
 
    const int bytestream_size= get32(buf+16);
333
 
    const int wordstream_size= get32(buf+12);
334
 
    
335
 
    if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length)
336
 
        printf("lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size, 
 
328
    const unsigned int bitstream_size= get32(buf+8);
 
329
    const unsigned int bytestream_size= get32(buf+16);
 
330
    const unsigned int wordstream_size= get32(buf+12);
 
331
 
 
332
    if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length
 
333
       || bitstream_size  > (1<<26)
 
334
       || bytestream_size > (1<<26)
 
335
       || wordstream_size > (1<<26)
 
336
       ){
 
337
        av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
337
338
        bitstream_size+ bytestream_size+ wordstream_size - length);
338
 
    
 
339
        return -1;
 
340
    }
 
341
 
339
342
    f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
340
343
    f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + 20), bitstream_size/4);
341
344
    init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
342
345
 
343
346
    f->wordstream= (uint16_t*)(buf + 20 + bitstream_size);
344
347
    f->bytestream= buf + 20 + bitstream_size + wordstream_size;
345
 
    
 
348
 
346
349
    init_mv(f);
347
 
    
 
350
 
348
351
    for(y=0; y<height; y+=8){
349
352
        for(x=0; x<width; x+=8){
350
353
            decode_p_block(f, dst + x, src + x, 3, 3, stride);
351
354
        }
352
 
        src += 8*stride; 
353
 
        dst += 8*stride; 
 
355
        src += 8*stride;
 
356
        dst += 8*stride;
354
357
    }
355
 
    
 
358
 
356
359
    if(bitstream_size != (get_bits_count(&f->gb)+31)/32*4)
357
 
        printf(" %d %d %d bytes left\n", 
358
 
            bitstream_size - (get_bits_count(&f->gb)+31)/32*4, 
 
360
        av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
 
361
            bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
359
362
            bytestream_size - (f->bytestream - (buf + 20 + bitstream_size + wordstream_size)),
360
363
            wordstream_size - (((uint8_t*)f->wordstream) - (buf + 20 + bitstream_size))
361
364
        );
362
 
    
 
365
 
363
366
    return 0;
364
367
}
365
368
 
373
376
    /* DC coef */
374
377
    val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
375
378
    if (val>>4){
376
 
        printf("error dc run != 0\n");
 
379
        av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
377
380
    }
378
381
 
379
382
    if(val)
386
389
    i = 1;
387
390
    for(;;) {
388
391
        code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
389
 
        
 
392
 
390
393
        /* EOB */
391
394
        if (code == 0)
392
395
            break;
396
399
            level = get_xbits(&f->gb, code & 0xf);
397
400
            i += code >> 4;
398
401
            if (i >= 64) {
399
 
                printf("run %d oveflow\n", i);
 
402
                av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
400
403
                return 0;
401
404
            }
402
405
 
416
419
    int stride= f->current_picture.linesize[0]>>1;
417
420
    int i;
418
421
    uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
419
 
    
 
422
 
420
423
    for(i=0; i<4; i++){
421
424
        block[i][0] += 0x80*8*8;
422
425
        idct(block[i]);
426
429
        for(i=4; i<6; i++) idct(block[i]);
427
430
    }
428
431
 
 
432
/* Note transform is:
 
433
y= ( 1b + 4g + 2r)/14
 
434
cb=( 3b - 2g - 1r)/14
 
435
cr=(-1b - 4g + 5r)/14
 
436
*/
429
437
    for(y=0; y<8; y++){
430
438
        for(x=0; x<8; x++){
431
439
            DCTELEM *temp= block[(x>>2) + 2*(y>>2)] + 2*(x&3) + 2*8*(y&3); //FIXME optimize
433
441
            int cr= block[5][x + 8*y];
434
442
            int cg= (cb + cr)>>1;
435
443
            int y;
436
 
            
 
444
 
437
445
            cb+=cb;
438
 
            
 
446
 
439
447
            y = temp[0];
440
448
            dst[0       ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
441
449
            y = temp[1];
452
460
 
453
461
static int decode_i_mb(FourXContext *f){
454
462
    int i;
455
 
    
 
463
 
456
464
    f->dsp.clear_blocks(f->block[0]);
457
 
    
 
465
 
458
466
    for(i=0; i<6; i++){
459
467
        if(decode_i_block(f, f->block[i]) < 0)
460
468
            return -1;
461
469
    }
462
 
    
 
470
 
463
471
    return 0;
464
472
}
465
473
 
472
480
    int start, end;
473
481
    uint8_t *ptr= buf;
474
482
    int j;
475
 
    
 
483
 
476
484
    memset(frequency, 0, sizeof(frequency));
477
485
    memset(up, -1, sizeof(up));
478
486
 
480
488
    end= *ptr++;
481
489
    for(;;){
482
490
        int i;
483
 
        
 
491
 
484
492
        for(i=start; i<=end; i++){
485
493
            frequency[i]= *ptr++;
486
 
//            printf("%d %d %d\n", start, end, frequency[i]);
487
494
        }
488
495
        start= *ptr++;
489
496
        if(start==0) break;
490
 
        
 
497
 
491
498
        end= *ptr++;
492
499
    }
493
500
    frequency[256]=1;
494
501
 
495
 
    while((ptr - buf)&3) ptr++; // 4byte align 
 
502
    while((ptr - buf)&3) ptr++; // 4byte align
496
503
 
497
 
//    for(j=0; j<16; j++)
498
 
//        printf("%2X", ptr[j]);
499
 
    
500
504
    for(j=257; j<512; j++){
501
505
        int min_freq[2]= {256*256, 256*256};
502
506
        int smallest[2]= {0, 0};
513
517
            }
514
518
        }
515
519
        if(min_freq[1] == 256*256) break;
516
 
        
 
520
 
517
521
        frequency[j]= min_freq[0] + min_freq[1];
518
522
        flag[ smallest[0] ]= 0;
519
523
        flag[ smallest[1] ]= 1;
520
 
        up[ smallest[0] ]= 
 
524
        up[ smallest[0] ]=
521
525
        up[ smallest[1] ]= j;
522
526
        frequency[ smallest[0] ]= frequency[ smallest[1] ]= 0;
523
527
    }
530
534
        for(node= j; up[node] != -1; node= up[node]){
531
535
            bits += flag[node]<<len;
532
536
            len++;
533
 
            if(len > 31) printf("vlc length overflow\n"); //can this happen at all ?
 
537
            if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n"); //can this happen at all ?
534
538
        }
535
 
        
 
539
 
536
540
        bits_tab[j]= bits;
537
541
        len_tab[j]= len;
538
542
    }
539
 
    
540
 
    init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257, 
 
543
 
 
544
    init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
541
545
             len_tab , 1, 1,
542
 
             bits_tab, 4, 4);
543
 
             
 
546
             bits_tab, 4, 4, 0);
 
547
 
544
548
    return ptr;
545
549
}
546
550
 
550
554
    const int height= f->avctx->height;
551
555
    uint16_t *dst= (uint16_t*)f->current_picture.data[0];
552
556
    const int stride= f->current_picture.linesize[0]>>1;
553
 
    const int bitstream_size= get32(buf);
 
557
    const unsigned int bitstream_size= get32(buf);
554
558
    const int token_count __attribute__((unused)) = get32(buf + bitstream_size + 8);
555
 
    int prestream_size= 4*get32(buf + bitstream_size + 4);
 
559
    unsigned int prestream_size= 4*get32(buf + bitstream_size + 4);
556
560
    uint8_t *prestream= buf + bitstream_size + 12;
557
 
    
558
 
    if(prestream_size + bitstream_size + 12 != length)
559
 
        fprintf(stderr, "size missmatch %d %d %d\n", prestream_size, bitstream_size, length);
560
 
   
 
561
 
 
562
    if(prestream_size + bitstream_size + 12 != length
 
563
       || bitstream_size > (1<<26)
 
564
       || prestream_size > (1<<26)){
 
565
        av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n", prestream_size, bitstream_size, length);
 
566
        return -1;
 
567
    }
 
568
 
561
569
    prestream= read_huffman_tables(f, prestream);
562
570
 
563
571
    init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
569
577
    init_get_bits(&f->pre_gb, f->bitstream_buffer, 8*prestream_size);
570
578
 
571
579
    f->last_dc= 0*128*8*8;
572
 
    
 
580
 
573
581
    for(y=0; y<height; y+=16){
574
582
        for(x=0; x<width; x+=16){
575
583
            if(decode_i_mb(f) < 0)
577
585
 
578
586
            idct_put(f, x, y);
579
587
        }
580
 
        dst += 16*stride; 
 
588
        dst += 16*stride;
581
589
    }
582
590
 
583
591
    if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
584
 
        printf("end missmatch\n");
585
 
    
 
592
        av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
 
593
 
586
594
    return 0;
587
595
}
588
596
 
589
 
static int decode_frame(AVCodecContext *avctx, 
 
597
static int decode_frame(AVCodecContext *avctx,
590
598
                        void *data, int *data_size,
591
599
                        uint8_t *buf, int buf_size)
592
600
{
595
603
    AVFrame *p, temp;
596
604
    int i, frame_4cc, frame_size;
597
605
 
598
 
    *data_size = 0;
599
 
 
600
 
    /* special case for last picture */
601
 
    if (buf_size == 0) {
602
 
        return 0;
603
 
    }
604
 
 
605
606
    frame_4cc= get32(buf);
606
 
    if(buf_size != get32(buf+4)+8){
607
 
        fprintf(stderr, "size missmatch %d %d\n", buf_size, get32(buf+4));
 
607
    if(buf_size != get32(buf+4)+8 || buf_size < 20){
 
608
        av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", buf_size, get32(buf+4));
608
609
    }
609
610
 
610
611
    if(frame_4cc == ff_get_fourcc("cfrm")){
616
617
 
617
618
        for(i=0; i<CFRAME_BUFFER_COUNT; i++){
618
619
            if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
619
 
                printf("lost c frame %d\n", f->cfrm[i].id);
 
620
                av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
620
621
        }
621
 
        
 
622
 
622
623
        for(i=0; i<CFRAME_BUFFER_COUNT; i++){
623
624
            if(f->cfrm[i].id   == id) break;
624
625
            if(f->cfrm[i].size == 0 ) free_index= i;
629
630
            f->cfrm[i].id= id;
630
631
        }
631
632
        cfrm= &f->cfrm[i];
632
 
        
 
633
 
633
634
        cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
634
 
        
 
635
        if(!cfrm->data){ //explicit check needed as memcpy below might not catch a NULL
 
636
            av_log(f->avctx, AV_LOG_ERROR, "realloc falure");
 
637
            return -1;
 
638
        }
 
639
 
635
640
        memcpy(cfrm->data + cfrm->size, buf+20, data_size);
636
641
        cfrm->size += data_size;
637
 
        
 
642
 
638
643
        if(cfrm->size >= whole_size){
639
644
            buf= cfrm->data;
640
645
            frame_size= cfrm->size;
641
 
            
 
646
 
642
647
            if(id != avctx->frame_number){
643
 
                printf("cframe id missmatch %d %d\n", id, avctx->frame_number);
 
648
                av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n", id, avctx->frame_number);
644
649
            }
645
 
            
 
650
 
646
651
            cfrm->size= cfrm->id= 0;
647
652
            frame_4cc= ff_get_fourcc("pfrm");
648
653
        }else
650
655
    }else{
651
656
        buf= buf + 12;
652
657
        frame_size= buf_size - 12;
653
 
    }    
 
658
    }
654
659
 
655
660
    temp= f->current_picture;
656
661
    f->current_picture= f->last_picture;
666
671
 
667
672
    p->reference= 1;
668
673
    if(avctx->get_buffer(avctx, p) < 0){
669
 
        fprintf(stderr, "get_buffer() failed\n");
 
674
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
670
675
        return -1;
671
676
    }
672
677
 
679
684
        if(decode_p_frame(f, buf, frame_size) < 0)
680
685
            return -1;
681
686
    }else if(frame_4cc == ff_get_fourcc("snd_")){
682
 
        printf("ignoring snd_ chunk length:%d\n", buf_size);
 
687
        av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
683
688
    }else{
684
 
        printf("ignoring unknown chunk length:%d\n", buf_size);
 
689
        av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
685
690
    }
686
691
 
687
 
#if 0
688
 
for(i=0; i<20; i++){
689
 
    printf("%2X %c ", buf[i], clip(buf[i],16,126));
690
 
}
691
 
#endif
692
 
 
693
692
    p->key_frame= p->pict_type == I_TYPE;
694
693
 
695
694
    *picture= *p;
696
695
    *data_size = sizeof(AVPicture);
697
696
 
698
697
    emms_c();
699
 
    
 
698
 
700
699
    return buf_size;
701
700
}
702
701
 
711
710
 
712
711
static int decode_init(AVCodecContext *avctx){
713
712
    FourXContext * const f = avctx->priv_data;
714
 
 
 
713
 
715
714
    common_init(avctx);
716
715
    init_vlcs(f);
717
716
 
732
731
        f->cfrm[i].allocated_size= 0;
733
732
    }
734
733
    free_vlc(&f->pre_vlc);
735
 
    
736
 
    avcodec_default_free_buffers(avctx);
737
734
 
738
735
    return 0;
739
736
}