Revision 11f18faf

View differences:

libavcodec/Makefile
16 16
      motion_est.o imgconvert.o imgresample.o msmpeg4.o \
17 17
      mpeg12.o h263dec.o svq1.o rv10.o mpegaudiodec.o pcm.o simple_idct.o \
18 18
      ratecontrol.o adpcm.o eval.o dv.o error_resilience.o \
19
      wmadec.o fft.o mdct.o mace.o
19
      wmadec.o fft.o mdct.o mace.o huffyuv.o
20 20
ASM_OBJS=
21 21

  
22 22
# currently using liba52 for ac3 decoding
libavcodec/allcodecs.c
53 53
    register_avcodec(&msmpeg4v3_encoder);
54 54
    register_avcodec(&wmv1_encoder);
55 55
    register_avcodec(&wmv2_encoder);
56
    register_avcodec(&huffyuv_encoder);
56 57
#endif /* CONFIG_ENCODERS */
57 58
    register_avcodec(&rawvideo_codec);
58 59

  
......
78 79
    register_avcodec(&wmav2_decoder);
79 80
    register_avcodec(&mace3_decoder);
80 81
    register_avcodec(&mace6_decoder);
82
    register_avcodec(&huffyuv_decoder);
81 83
#ifdef CONFIG_AC3
82 84
    register_avcodec(&ac3_decoder);
83 85
#endif
libavcodec/avcodec.h
5 5

  
6 6
#define LIBAVCODEC_VERSION_INT 0x000406
7 7
#define LIBAVCODEC_VERSION     "0.4.6"
8
#define LIBAVCODEC_BUILD       4638
9
#define LIBAVCODEC_BUILD_STR   "4638"
8
#define LIBAVCODEC_BUILD       4639
9
#define LIBAVCODEC_BUILD_STR   "4639"
10 10

  
11 11
enum CodecID {
12 12
    CODEC_ID_NONE, 
......
34 34
    CODEC_ID_WMAV2,
35 35
    CODEC_ID_MACE3,
36 36
    CODEC_ID_MACE6,
37
    CODEC_ID_HUFFYUV,
37 38

  
38 39
    /* various pcm "codecs" */
39 40
    CODEC_ID_PCM_S16LE,
......
770 771
     * CPU features (i.e. MMX, SSE. ...)
771 772
     */
772 773
     unsigned dsp_mask;
774

  
775
    /**
776
     * bits per sample/pixel from the demuxer (needed for huffyuv)
777
     * encoding; set by lavc
778
     * decoding: set by user
779
     */
780
     int bits_per_sample;
781
    
782
    /**
783
     * prediction method (needed for huffyuv)
784
     * encoding; set by user
785
     * decoding: unused
786
     */
787
     int prediction_method;
788
#define FF_PRED_LEFT   0
789
#define FF_PRED_PLANE  1
790
#define FF_PRED_MEDIAN 2
773 791
} AVCodecContext;
774 792

  
775 793
typedef struct AVCodec {
......
810 828
extern AVCodec msmpeg4v3_encoder;
811 829
extern AVCodec wmv1_encoder;
812 830
extern AVCodec wmv2_encoder;
831
extern AVCodec huffyuv_encoder;
813 832

  
814 833
extern AVCodec h263_decoder;
815 834
extern AVCodec mpeg4_decoder;
......
831 850
extern AVCodec mp3_decoder;
832 851
extern AVCodec mace3_decoder;
833 852
extern AVCodec mace6_decoder;
853
extern AVCodec huffyuv_decoder;
834 854

  
835 855
/* pcm codecs */
836 856
#define PCM_CODEC(id, name) \
libavcodec/dsputil.c
1342 1342
    memset(blocks, 0, sizeof(DCTELEM)*6*64);
1343 1343
}
1344 1344

  
1345
static void add_bytes_c(uint8_t *dst, uint8_t *src, int w){
1346
    int i;
1347
    for(i=0; i+7<w; i++){
1348
        dst[i+0] += src[i+0];
1349
        dst[i+1] += src[i+1];
1350
        dst[i+2] += src[i+2];
1351
        dst[i+3] += src[i+3];
1352
        dst[i+4] += src[i+4];
1353
        dst[i+5] += src[i+5];
1354
        dst[i+6] += src[i+6];
1355
        dst[i+7] += src[i+7];
1356
    }
1357
    for(; i<w; i++)
1358
        dst[i+0] += src[i+0];
1359
}
1360

  
1361
static void diff_bytes_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){
1362
    int i;
1363
    for(i=0; i+7<w; i++){
1364
        dst[i+0] = src1[i+0]-src2[i+0];
1365
        dst[i+1] = src1[i+1]-src2[i+1];
1366
        dst[i+2] = src1[i+2]-src2[i+2];
1367
        dst[i+3] = src1[i+3]-src2[i+3];
1368
        dst[i+4] = src1[i+4]-src2[i+4];
1369
        dst[i+5] = src1[i+5]-src2[i+5];
1370
        dst[i+6] = src1[i+6]-src2[i+6];
1371
        dst[i+7] = src1[i+7]-src2[i+7];
1372
    }
1373
    for(; i<w; i++)
1374
        dst[i+0] = src1[i+0]-src2[i+0];
1375
}
1376

  
1345 1377
void dsputil_init(DSPContext* c, unsigned mask)
1346 1378
{
1347 1379
    static int init_done = 0;
......
1431 1463
    /* dspfunc(avg_no_rnd_qpel, 1, 8); */
1432 1464
#undef dspfunc
1433 1465

  
1466
    c->add_bytes= add_bytes_c;
1467
    c->diff_bytes= diff_bytes_c;
1468

  
1434 1469
#ifdef HAVE_MMX
1435 1470
    dsputil_init_mmx(c, mask);
1436 1471
    if (ff_bit_exact)
libavcodec/dsputil.h
117 117
    op_pixels_abs_func pix_abs8x8_x2;
118 118
    op_pixels_abs_func pix_abs8x8_y2;
119 119
    op_pixels_abs_func pix_abs8x8_xy2;
120
    
121
    /* huffyuv specific */
122
    //FIXME note: alignment isnt guranteed currently but could be if needed
123
    void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
124
    void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 16*/,int w);
120 125
} DSPContext;
121 126

  
122 127
void dsputil_init(DSPContext* p, unsigned mask);
libavcodec/huffyuv.c
1
/*
2
 * huffyuv codec for libavcodec
3
 *
4
 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 *
20
 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
21
 * the algorithm used 
22
 */
23

  
24
#include "common.h"
25
#include "avcodec.h"
26
#include "dsputil.h"
27

  
28
#ifndef MAX_INT64
29
#define MAX_INT64 9223372036854775807LL
30
#endif
31

  
32
#define VLC_BITS 11
33
  
34
typedef enum Predictor{
35
    LEFT= 0,
36
    PLANE,
37
    MEDIAN,
38
} Predictor;
39
 
40
typedef struct HYuvContext{
41
    AVCodecContext *avctx;
42
    Predictor predictor;
43
    GetBitContext gb;
44
    PutBitContext pb;
45
    int interlaced;
46
    int decorrelate;
47
    int bitstream_bpp;
48
    int version;
49
    int yuy2;                               //use yuy2 instead of 422P
50
    int bgr32;                              //use bgr32 instead of bgr24
51
    int width, height;
52
    int flags;
53
    int picture_number;
54
    int linesize[3];
55
    uint8_t __align8 temp[3][2500];
56
    uint64_t stats[3][256];
57
    uint8_t len[3][256];
58
    uint32_t bits[3][256];
59
    VLC vlc[3];
60
    uint8_t __align8 *picture[3];
61
    uint8_t __align8 bitstream_buffer[1024*1024*3]; //FIXME dynamic alloc or some other solution
62
    DSPContext dsp; 
63
}HYuvContext;
64

  
65
static inline void bswap_buf(uint32_t *dst, uint32_t *src, int w){
66
    int i;
67
    
68
    for(i=0; i+8<=w; i+=8){
69
        dst[i+0]= bswap_32(src[i+0]);
70
        dst[i+1]= bswap_32(src[i+1]);
71
        dst[i+2]= bswap_32(src[i+2]);
72
        dst[i+3]= bswap_32(src[i+3]);
73
        dst[i+4]= bswap_32(src[i+4]);
74
        dst[i+5]= bswap_32(src[i+5]);
75
        dst[i+6]= bswap_32(src[i+6]);
76
        dst[i+7]= bswap_32(src[i+7]);
77
    }
78
    for(;i<w; i++){
79
        dst[i+0]= bswap_32(src[i+0]);
80
    }
81
}
82

  
83
static inline int add_left_prediction(uint8_t *dst, uint8_t *src, int w, int acc){
84
    int i;
85

  
86
    for(i=0; i<w-1; i++){
87
        acc+= src[i];
88
        dst[i]= acc;
89
        i++;
90
        acc+= src[i];
91
        dst[i]= acc;
92
    }
93

  
94
    for(; i<w; i++){
95
        acc+= src[i];
96
        dst[i]= acc;
97
    }
98

  
99
    return acc;
100
}
101

  
102
static inline void add_median_prediction(uint8_t *dst, uint8_t *src1, uint8_t *diff, int w, int *left, int *left_top){
103
    int i;
104
    uint8_t l, lt;
105

  
106
    l= *left;
107
    lt= *left_top;
108

  
109
    for(i=0; i<w; i++){
110
        l= mid_pred(l, src1[i], (l + src1[i] - lt)&0xFF) + diff[i];
111
        lt= src1[i];
112
        dst[i]= l;
113
    }    
114

  
115
    *left= l;
116
    *left_top= lt;
117
}
118
//FIXME optimize
119
static inline void sub_median_prediction(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top){
120
    int i;
121
    uint8_t l, lt;
122

  
123
    l= *left;
124
    lt= *left_top;
125

  
126
    for(i=0; i<w; i++){
127
        const int pred= mid_pred(l, src1[i], (l + src1[i] - lt)&0xFF);
128
        lt= src1[i];
129
        l= src2[i];
130
        dst[i]= l - pred;
131
    }    
132

  
133
    *left= l;
134
    *left_top= lt;
135
}
136

  
137

  
138
static inline void add_left_prediction_bgr32(uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){
139
    int i;
140
    int r,g,b;
141
    r= *red;
142
    g= *green;
143
    b= *blue;
144

  
145
    for(i=0; i<w; i++){
146
        b+= src[4*i+0];
147
        g+= src[4*i+1];
148
        r+= src[4*i+2];
149
        
150
        dst[4*i+0]= b;
151
        dst[4*i+1]= g;
152
        dst[4*i+2]= r;
153
    }
154

  
155
    *red= r;
156
    *green= g;
157
    *blue= b;
158
}
159

  
160
//FIXME optimize
161
static inline int sub_left_prediction(uint8_t *dst, uint8_t *src, int w, int left){
162
    int i;
163
    for(i=0; i<w; i++){
164
        const int temp= src[i];
165
        dst[i]= temp - left;
166
        left= temp;
167
    }
168
    return left;
169
}
170

  
171
static void read_len_table(uint8_t *dst, GetBitContext *gb){
172
    int i, val, repeat;
173
  
174
    for(i=0; i<256;){
175
        repeat= get_bits(gb, 3);
176
        val   = get_bits(gb, 5);
177
        if(repeat==0)
178
            repeat= get_bits(gb, 8);
179
//printf("%d %d\n", val, repeat);
180
        while (repeat--)
181
            dst[i++] = val;
182
    }
183
}
184

  
185
static int generate_bits_table(uint32_t *dst, uint8_t *len_table){
186
    int len, index;
187
    uint32_t bits=0;
188

  
189
    for(len=32; len>0; len--){
190
        int bit= 1<<(32-len);
191
        for(index=0; index<256; index++){
192
            if(len_table[index]==len){
193
                if(bits & (bit-1)){
194
                    fprintf(stderr, "Error generating huffman table\n");
195
                    return -1;
196
                }
197
                dst[index]= bits>>(32-len);
198
                bits+= bit;
199
            }
200
        }
201
    }
202
    return 0;
203
}
204

  
205
static void generate_len_table(uint8_t *dst, uint64_t *stats, int size){
206
    uint64_t counts[2*size];
207
    int up[2*size];
208
    int offset, i, next;
209
    
210
    for(offset=1; ; offset<<=1){
211
        for(i=0; i<size; i++){
212
            counts[i]= stats[i] + offset - 1;
213
        }
214
        
215
        for(next=size; next<size*2; next++){
216
            uint64_t min1, min2;
217
            int min1_i, min2_i;
218
            
219
            min1=min2= INT64_MAX;
220
            min1_i= min2_i=-1;
221
            
222
            for(i=0; i<next; i++){
223
                if(min2 > counts[i]){
224
                    if(min1 > counts[i]){
225
                        min2= min1;
226
                        min2_i= min1_i;
227
                        min1= counts[i];
228
                        min1_i= i;
229
                    }else{
230
                        min2= counts[i];
231
                        min2_i= i;
232
                    }
233
                }
234
            }
235
            
236
            if(min2==INT64_MAX) break;
237
            
238
            counts[next]= min1 + min2;
239
            counts[min1_i]=
240
            counts[min2_i]= MAX_INT64;
241
            up[min1_i]=
242
            up[min2_i]= next;
243
            up[next]= -1;
244
        }
245
        
246
        for(i=0; i<size; i++){
247
            int len;
248
            int index=i;
249
            
250
            for(len=0; up[index] != -1; len++)
251
                index= up[index];
252
                
253
            if(len > 32) break;
254
            
255
            dst[i]= len;
256
        }
257
        if(i==size) break;
258
    }
259
}
260

  
261
static int read_huffman_tables(HYuvContext *s, uint8_t *src, int length){
262
    GetBitContext gb;
263
    int i;
264
    
265
    init_get_bits(&gb, src, length);
266
    
267
    for(i=0; i<3; i++){
268
        read_len_table(s->len[i], &gb);
269
        
270
        if(generate_bits_table(s->bits[i], s->len[i])<0){
271
            return -1;
272
        }
273
#if 0
274
for(j=0; j<256; j++){
275
printf("%6X, %2d,  %3d\n", s->bits[i][j], s->len[i][j], j);
276
}
277
#endif
278
        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
279
    }
280
    
281
    return 0;
282
}
283

  
284
static int read_old_huffman_tables(HYuvContext *s){
285
#if 0    
286
    GetBitContext gb;
287
    int i;
288

  
289
    init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma));
290
    read_len_table(s->len[0], &gb);
291
    init_get_bits(&gb, classic_shift_chroma, sizeof(classic_shift_chroma));
292
    read_len_table(s->len[1], &gb);
293
    
294
    for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma  [i];
295
    for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i];
296

  
297
    if(s->bitstream_bpp >= 24){
298
        memcpy(s->bits[1], s->bits[0], 256*sizeof(uint32_t));
299
        memcpy(s->len[1] , s->len [0], 256*sizeof(uint8_t));
300
    }
301
    memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t));
302
    memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t));
303
    
304
    for(i=0; i<3; i++)
305
        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
306
    
307
    return 0;
308
#else
309
    fprintf(stderr, "v1 huffyuv is not supported \n");
310
    return -1;
311
#endif
312
}
313

  
314
static int decode_init(AVCodecContext *avctx)
315
{
316
    HYuvContext *s = avctx->priv_data;
317
    int width, height, y_size, c_size, stride;
318

  
319
    s->avctx= avctx;
320
    s->flags= avctx->flags;
321
        
322
    dsputil_init(&s->dsp, avctx->dsp_mask);
323
    
324
    width= s->width= avctx->width;
325
    height= s->height= avctx->height;
326
s->bgr32=1;
327
    assert(width && height);
328
//if(avctx->extradata)
329
//  printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size);
330
    if(avctx->extradata_size){
331
        if(avctx->bits_per_sample&7)
332
            s->version=1; // do such files exist at all?
333
        else
334
            s->version=2;
335
    }else
336
        s->version=0;
337
    
338
    if(s->version==2){
339
        int method;
340

  
341
        method= ((uint8_t*)avctx->extradata)[0];
342
        s->decorrelate= method&64 ? 1 : 0;
343
        s->predictor= method&63;
344
        s->bitstream_bpp= ((uint8_t*)avctx->extradata)[1];
345
        if(s->bitstream_bpp==0) 
346
            s->bitstream_bpp= avctx->bits_per_sample&~7;
347
            
348
        if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0)
349
            return -1;
350
    }else{
351
        switch(avctx->bits_per_sample&7){
352
        case 1:
353
            s->predictor= LEFT;
354
            s->decorrelate= 0;
355
            break;
356
        case 2:
357
            s->predictor= LEFT;
358
            s->decorrelate= 1;
359
            break;
360
        case 3:
361
            s->predictor= PLANE;
362
            s->decorrelate= avctx->bits_per_sample >= 24;
363
            break;
364
        case 4:
365
            s->predictor= MEDIAN;
366
            s->decorrelate= 0;
367
            break;
368
        default:
369
            s->predictor= LEFT; //OLD
370
            s->decorrelate= 0;
371
            break;
372
        }
373
        s->bitstream_bpp= avctx->bits_per_sample & ~7;
374
        
375
        if(read_old_huffman_tables(s) < 0)
376
            return -1;
377
    }
378
    
379
    s->interlaced= height > 288;
380
    
381
    c_size= 0;
382
    switch(s->bitstream_bpp){
383
    case 12:
384
        avctx->pix_fmt = PIX_FMT_YUV420P;
385
        stride= (width+15)&~15;
386
        c_size= height*stride/4;
387
        break;
388
    case 16:
389
        if(s->yuy2){
390
            avctx->pix_fmt = PIX_FMT_YUV422;
391
            stride= (width*2+15)&~15;
392
        }else{
393
            avctx->pix_fmt = PIX_FMT_YUV422P;
394
            stride= (width+15)&~15;
395
            c_size= height*stride/2;
396
        }
397
        break;
398
    case 24:
399
    case 32:
400
        if(s->bgr32){
401
            avctx->pix_fmt = PIX_FMT_BGRA32;
402
            stride= (width*4+15)&~15;
403
        }else{
404
            avctx->pix_fmt = PIX_FMT_BGR24;
405
            stride= (width*3+15)&~15;
406
        }
407
        break;
408
    default:
409
        assert(0);
410
        stride=0; //gcc fix
411
    }
412
    
413
    y_size= height*stride;
414
    
415
    s->linesize[0]= stride;
416
    s->picture[0]= av_mallocz(y_size);
417
 
418
    if(c_size){
419
        s->picture[1]= av_mallocz(c_size);
420
        s->picture[2]= av_mallocz(c_size);
421
        s->linesize[1]= s->linesize[2]= stride/2;
422
        
423
        memset(s->picture[1], 128, c_size);
424
        memset(s->picture[2], 128, c_size);
425
    }
426
    
427
//    printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced);
428
    
429
    return 0;
430
}
431

  
432
static void store_table(HYuvContext *s, uint8_t *len){
433
    int i;
434
    int index= s->avctx->extradata_size;
435

  
436
    for(i=0; i<256;){
437
        int cur=i;
438
        int val= len[i];
439
        int repeat;
440
        
441
        for(; i<256 && len[i]==val; i++);
442
        
443
        repeat= i - cur;
444
        
445
        if(repeat>7){
446
            ((uint8_t*)s->avctx->extradata)[index++]= val;
447
            ((uint8_t*)s->avctx->extradata)[index++]= repeat;
448
        }else{
449
            ((uint8_t*)s->avctx->extradata)[index++]= val | (repeat<<5);
450
        }
451
    }
452
    
453
    s->avctx->extradata_size= index;
454
}
455

  
456
static int encode_init(AVCodecContext *avctx)
457
{
458
    HYuvContext *s = avctx->priv_data;
459
    int i, j, width, height;
460

  
461
    s->avctx= avctx;
462
    s->flags= avctx->flags;
463
        
464
    dsputil_init(&s->dsp, avctx->dsp_mask);
465
    
466
    width= s->width= avctx->width;
467
    height= s->height= avctx->height;
468
    
469
    assert(width && height);
470
    
471
    avctx->extradata= av_mallocz(1024*10);
472
    avctx->stats_out= av_mallocz(1024*10);
473
    s->version=2;
474
    
475
    switch(avctx->pix_fmt){
476
    case PIX_FMT_YUV422P:
477
        s->bitstream_bpp= 16;
478
        break;
479
    default:
480
        fprintf(stderr, "format not supported\n");
481
        return -1;
482
    }
483
    avctx->bits_per_sample= s->bitstream_bpp;
484
    s->decorrelate= s->bitstream_bpp >= 24;
485
    s->predictor= avctx->prediction_method;
486
    
487
    ((uint8_t*)avctx->extradata)[0]= s->predictor;
488
    ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp;
489
    ((uint8_t*)avctx->extradata)[2]=
490
    ((uint8_t*)avctx->extradata)[3]= 0;
491
    s->avctx->extradata_size= 4;
492
    
493
    if(avctx->stats_in){
494
        char *p= avctx->stats_in;
495
    
496
        for(i=0; i<3; i++)
497
            for(j=0; j<256; j++)
498
                s->stats[i][j]= 1;
499

  
500
        for(;;){
501
            for(i=0; i<3; i++){
502
                char *next;
503

  
504
                for(j=0; j<256; j++){
505
                    s->stats[i][j]+= strtol(p, &next, 0);
506
                    if(next==p) return -1;
507
                    p=next;
508
                }        
509
            }
510
            if(p[0]==0 || p[1]==0 || p[2]==0) break;
511
        }
512
    }else{
513
        for(i=0; i<3; i++)
514
            for(j=0; j<256; j++){
515
                int d= FFMIN(j, 256-j);
516
                
517
                s->stats[i][j]= 100000000/(d+1);
518
            }
519
    }
520
    
521
    for(i=0; i<3; i++){
522
        generate_len_table(s->len[i], s->stats[i], 256);
523

  
524
        if(generate_bits_table(s->bits[i], s->len[i])<0){
525
            return -1;
526
        }
527
        
528
        store_table(s, s->len[i]);
529
    }
530

  
531
    for(i=0; i<3; i++)
532
        for(j=0; j<256; j++)
533
            s->stats[i][j]= 0;
534
    
535
    s->interlaced= height > 288;
536
    
537
//    printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced);
538
    
539
    s->picture_number=0;
540
    
541
    return 0;
542
}
543

  
544
static void decode_422_bitstream(HYuvContext *s, int count){
545
    int i;
546
    
547
    count/=2;
548
    
549
    for(i=0; i<count; i++){
550
        s->temp[0][2*i  ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); 
551
        s->temp[1][  i  ]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); 
552
        s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); 
553
        s->temp[2][  i  ]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); 
554
    }
555
}
556

  
557
static void encode_422_bitstream(HYuvContext *s, int count){
558
    int i;
559
    
560
    count/=2;
561
    if(s->flags&CODEC_FLAG_PASS1){
562
        for(i=0; i<count; i++){
563
            s->stats[0][ s->temp[0][2*i  ] ]++;
564
            s->stats[1][ s->temp[1][  i  ] ]++;
565
            s->stats[0][ s->temp[0][2*i+1] ]++;
566
            s->stats[2][ s->temp[2][  i  ] ]++;
567
        }
568
    }else{
569
        for(i=0; i<count; i++){
570
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i  ] ], s->bits[0][ s->temp[0][2*i  ] ]);
571
            put_bits(&s->pb, s->len[1][ s->temp[1][  i  ] ], s->bits[1][ s->temp[1][  i  ] ]);
572
            put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]);
573
            put_bits(&s->pb, s->len[2][ s->temp[2][  i  ] ], s->bits[2][ s->temp[2][  i  ] ]);
574
        }
575
    }
576
}
577

  
578
static void decode_bgr_bitstream(HYuvContext *s, int count){
579
    int i;
580
    
581
    if(s->decorrelate){
582
        if(s->bitstream_bpp==24){
583
            for(i=0; i<count; i++){
584
                s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); 
585
                s->temp[0][4*i  ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1];
586
                s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1];
587
            }
588
        }else{
589
            for(i=0; i<count; i++){
590
                s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); 
591
                s->temp[0][4*i  ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1];
592
                s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1]; 
593
                                   get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?!
594
            }
595
        }
596
    }else{
597
        if(s->bitstream_bpp==24){
598
            for(i=0; i<count; i++){
599
                s->temp[0][4*i  ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
600
                s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); 
601
                s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); 
602
            }
603
        }else{
604
            for(i=0; i<count; i++){
605
                s->temp[0][4*i  ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
606
                s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); 
607
                s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); 
608
                                   get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?!
609
            }
610
        }
611
    }
612
}
613

  
614
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
615
    HYuvContext *s = avctx->priv_data;
616
    const int width= s->width;
617
    const int width2= s->width>>1;
618
    const int height= s->height;
619
    const int fake_ystride= s->interlaced ? s->linesize[0]*2  : s->linesize[0];
620
    const int fake_ustride= s->interlaced ? s->linesize[1]*2  : s->linesize[1];
621
    const int fake_vstride= s->interlaced ? s->linesize[2]*2  : s->linesize[2];
622
    int i;
623

  
624
    AVPicture *picture = data;
625

  
626
    *data_size = 0;
627

  
628
    /* no supplementary picture */
629
    if (buf_size == 0)
630
        return 0;
631

  
632
    bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4);
633
    
634
    init_get_bits(&s->gb, s->bitstream_buffer, buf_size);
635
    
636
    if(s->bitstream_bpp<24){
637
        int y;
638
        int lefty, leftu, leftv;
639
        int lefttopy, lefttopu, lefttopv;
640
        
641
        if(s->yuy2){
642
            s->picture[0][3]= get_bits(&s->gb, 8);
643
            s->picture[0][2]= get_bits(&s->gb, 8);
644
            s->picture[0][1]= get_bits(&s->gb, 8);
645
            s->picture[0][0]= get_bits(&s->gb, 8);
646
            
647
            fprintf(stderr, "YUY2 output isnt implemenetd yet\n");
648
            return -1;
649
        }else{
650
        
651
            leftv= s->picture[2][0]= get_bits(&s->gb, 8);
652
            lefty= s->picture[0][1]= get_bits(&s->gb, 8);
653
            leftu= s->picture[1][0]= get_bits(&s->gb, 8);
654
                   s->picture[0][0]= get_bits(&s->gb, 8);
655
        
656
            switch(s->predictor){
657
            case LEFT:
658
            case PLANE:
659
                decode_422_bitstream(s, width-2);
660
                lefty= add_left_prediction(s->picture[0] + 2, s->temp[0], width-2, lefty);
661
                if(!(s->flags&CODEC_FLAG_GRAY)){
662
                    leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu);
663
                    leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv);
664
                }
665

  
666
                for(y=1; y<s->height; y++){
667
                    uint8_t *ydst, *udst, *vdst;
668
                    decode_422_bitstream(s, width);
669
                    
670
                    ydst= s->picture[0] + s->linesize[0]*y;
671
                    udst= s->picture[1] + s->linesize[1]*y;
672
                    vdst= s->picture[2] + s->linesize[2]*y;
673

  
674
                    lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
675
                    if(!(s->flags&CODEC_FLAG_GRAY)){
676
                        leftu= add_left_prediction(udst, s->temp[1], width2, leftu);
677
                        leftv= add_left_prediction(vdst, s->temp[2], width2, leftv);
678
                    }
679
                    if(s->predictor == PLANE){
680
                        if(y>s->interlaced){
681
                            s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
682
                            if(!(s->flags&CODEC_FLAG_GRAY)){
683
                                s->dsp.add_bytes(udst, udst - fake_ustride, width2);
684
                                s->dsp.add_bytes(vdst, vdst - fake_vstride, width2);
685
                            }
686
                        }
687
                    }
688
                }
689
                break;
690
            case MEDIAN:
691
                /* first line except first 2 pixels is left predicted */
692
                decode_422_bitstream(s, width-2);
693
                lefty= add_left_prediction(s->picture[0] + 2, s->temp[0], width-2, lefty);
694
                if(!(s->flags&CODEC_FLAG_GRAY)){
695
                    leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu);
696
                    leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv);
697
                }
698
                
699
                y=1;
700
                
701
                /* second line is left predicted for interlaced case */
702
                if(s->interlaced){
703
                    decode_422_bitstream(s, width);
704
                    lefty= add_left_prediction(s->picture[0] + s->linesize[0], s->temp[0], width, lefty);
705
                    if(!(s->flags&CODEC_FLAG_GRAY)){
706
                        leftu= add_left_prediction(s->picture[1] + s->linesize[2], s->temp[1], width2, leftu);
707
                        leftv= add_left_prediction(s->picture[2] + s->linesize[1], s->temp[2], width2, leftv);
708
                    }
709
                    y++;
710
                }
711

  
712
                /* next 4 pixels are left predicted too */
713
                decode_422_bitstream(s, 4);
714
                lefty= add_left_prediction(s->picture[0] + fake_ystride, s->temp[0], 4, lefty);
715
                if(!(s->flags&CODEC_FLAG_GRAY)){
716
                    leftu= add_left_prediction(s->picture[1] + fake_ustride, s->temp[1], 2, leftu);
717
                    leftv= add_left_prediction(s->picture[2] + fake_vstride, s->temp[2], 2, leftv);
718
                }
719

  
720
                /* next line except the first 4 pixels is median predicted */
721
                lefttopy= s->picture[0][3];
722
                decode_422_bitstream(s, width-4);
723
                add_median_prediction(s->picture[0] + fake_ystride+4, s->picture[0]+4, s->temp[0], width-4, &lefty, &lefttopy);
724
                if(!(s->flags&CODEC_FLAG_GRAY)){
725
                    lefttopu= s->picture[1][1];
726
                    lefttopv= s->picture[2][1];
727
                    add_median_prediction(s->picture[1] + fake_ustride+2, s->picture[1]+2, s->temp[1], width2-2, &leftu, &lefttopu);
728
                    add_median_prediction(s->picture[2] + fake_vstride+2, s->picture[2]+2, s->temp[2], width2-2, &leftv, &lefttopv);
729
                }
730
                y++;
731

  
732
                for(; y<height; y++){
733
                    uint8_t *ydst, *udst, *vdst;
734
                    decode_422_bitstream(s, width);
735
                    
736
                    ydst= s->picture[0] + s->linesize[0]*y;
737
                    udst= s->picture[1] + s->linesize[1]*y;
738
                    vdst= s->picture[2] + s->linesize[2]*y;
739

  
740
                    add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
741
                    if(!(s->flags&CODEC_FLAG_GRAY)){
742
                        add_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
743
                        add_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
744
                    }
745
                }
746
                break;
747
            }
748
        }
749
    }else{
750
        int y;
751
        int leftr, leftg, leftb;
752
        const int last_line= (height-1)*s->linesize[0];
753
        
754
        if(s->bitstream_bpp==32){
755
                   s->picture[0][last_line+3]= get_bits(&s->gb, 8);
756
            leftr= s->picture[0][last_line+2]= get_bits(&s->gb, 8);
757
            leftg= s->picture[0][last_line+1]= get_bits(&s->gb, 8);
758
            leftb= s->picture[0][last_line+0]= get_bits(&s->gb, 8);
759
        }else{
760
            leftr= s->picture[0][last_line+2]= get_bits(&s->gb, 8);
761
            leftg= s->picture[0][last_line+1]= get_bits(&s->gb, 8);
762
            leftb= s->picture[0][last_line+0]= get_bits(&s->gb, 8);
763
            skip_bits(&s->gb, 8);
764
        }
765
        
766
        if(s->bgr32){
767
            switch(s->predictor){
768
            case LEFT:
769
            case PLANE:
770
                decode_bgr_bitstream(s, width-1);
771
                add_left_prediction_bgr32(s->picture[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb);
772

  
773
                for(y=s->height-2; y>=0; y--){ //yes its stored upside down
774
                    decode_bgr_bitstream(s, width);
775
                    
776
                    add_left_prediction_bgr32(s->picture[0] + s->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb);
777
                    if(s->predictor == PLANE){
778
                        if((y&s->interlaced)==0){
779
                            s->dsp.add_bytes(s->picture[0] + s->linesize[0]*y, 
780
                                             s->picture[0] + s->linesize[0]*y + fake_ystride, fake_ystride);
781
                        }
782
                    }
783
                }
784
                break;
785
            default:
786
                fprintf(stderr, "prediction type not supported!\n");
787
            }
788
        }else{
789

  
790
            fprintf(stderr, "BGR24 output isnt implemenetd yet\n");
791
            return -1;
792
        }
793
    }
794
    emms_c();
795
    
796
    for(i=0;i<3;i++) {
797
        picture->data[i] = s->picture[i];
798
        picture->linesize[i]= s->linesize[i];
799
    }
800

  
801
    *data_size = sizeof(AVPicture);
802
    
803
    return (get_bits_count(&s->gb)+7)>>3;
804
}
805

  
806
static int decode_end(AVCodecContext *avctx)
807
{
808
    HYuvContext *s = avctx->priv_data;
809
    int i;
810
    
811
    for(i=0; i<3; i++){
812
        av_freep(&s->picture[i]);
813
        free_vlc(&s->vlc[i]);
814
    }
815

  
816
    return 0;
817
}
818

  
819
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
820
    HYuvContext *s = avctx->priv_data;
821
    AVPicture *pict = data;
822
    const int width= s->width;
823
    const int width2= s->width>>1;
824
    const int height= s->height;
825
    const int fake_ystride= s->interlaced ? pict->linesize[0]*2  : pict->linesize[0];
826
    const int fake_ustride= s->interlaced ? pict->linesize[1]*2  : pict->linesize[1];
827
    const int fake_vstride= s->interlaced ? pict->linesize[2]*2  : pict->linesize[2];
828
    int i, size;
829

  
830
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
831
    
832
    for(i=0; i<3; i++){
833
        s->picture[i]= pict->data[i];
834
        s->linesize[i]= pict->linesize[i];
835
    }
836
    
837
    if(avctx->pix_fmt == PIX_FMT_YUV422P){
838
        int lefty, leftu, leftv, y;
839

  
840
        put_bits(&s->pb, 8, leftv= s->picture[2][0]);
841
        put_bits(&s->pb, 8, lefty= s->picture[0][1]);
842
        put_bits(&s->pb, 8, leftu= s->picture[1][0]);
843
        put_bits(&s->pb, 8,        s->picture[0][0]);
844
        
845
        lefty= sub_left_prediction(s->temp[0], s->picture[0]+2, width-2 , lefty);
846
        leftu= sub_left_prediction(s->temp[1], s->picture[1]+1, width2-1, leftu);
847
        leftv= sub_left_prediction(s->temp[2], s->picture[2]+1, width2-1, leftv);
848
        
849
        encode_422_bitstream(s, width-2);
850
        
851
        if(s->predictor==MEDIAN){
852
            int lefttopy, lefttopu, lefttopv;
853
            y=1;
854
            if(s->interlaced){
855
                lefty= sub_left_prediction(s->temp[0], s->picture[0]+s->linesize[0], width , lefty);
856
                leftu= sub_left_prediction(s->temp[1], s->picture[1]+s->linesize[1], width2, leftu);
857
                leftv= sub_left_prediction(s->temp[2], s->picture[2]+s->linesize[2], width2, leftv);
858
        
859
                encode_422_bitstream(s, width);
860
                y++;
861
            }
862
            
863
            lefty= sub_left_prediction(s->temp[0], s->picture[0]+fake_ystride, 4, lefty);
864
            leftu= sub_left_prediction(s->temp[1], s->picture[1]+fake_ystride, 2, leftu);
865
            leftv= sub_left_prediction(s->temp[2], s->picture[2]+fake_ystride, 2, leftv);
866
        
867
            encode_422_bitstream(s, 4);
868
                        
869
            lefttopy= s->picture[0][3];
870
            lefttopu= s->picture[1][1];
871
            lefttopv= s->picture[2][1];
872
            sub_median_prediction(s->temp[0], s->picture[0]+4, s->picture[0] + fake_ystride+4, width-4 , &lefty, &lefttopy);
873
            sub_median_prediction(s->temp[1], s->picture[1]+2, s->picture[1] + fake_ustride+2, width2-2, &leftu, &lefttopu);
874
            sub_median_prediction(s->temp[2], s->picture[2]+2, s->picture[2] + fake_vstride+2, width2-2, &leftv, &lefttopv);
875
            encode_422_bitstream(s, width-4);
876
            y++;
877

  
878
            for(; y<height; y++){
879
                uint8_t *ydst, *udst, *vdst;
880
                    
881
                ydst= s->picture[0] + s->linesize[0]*y;
882
                udst= s->picture[1] + s->linesize[1]*y;
883
                vdst= s->picture[2] + s->linesize[2]*y;
884

  
885
                sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
886
                sub_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
887
                sub_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
888

  
889
                encode_422_bitstream(s, width);
890
            }
891
        }else{
892
            for(y=1; y<height; y++){
893
                uint8_t *ydst, *udst, *vdst;
894
                    
895
                ydst= s->picture[0] + s->linesize[0]*y;
896
                udst= s->picture[1] + s->linesize[1]*y;
897
                vdst= s->picture[2] + s->linesize[2]*y;
898

  
899
                if(s->predictor == PLANE && s->interlaced < y){
900
                    s->dsp.diff_bytes(s->temp[0], ydst, ydst - fake_ystride, width);
901
                    s->dsp.diff_bytes(s->temp[1], udst, udst - fake_ustride, width2);
902
                    s->dsp.diff_bytes(s->temp[2], vdst, vdst - fake_vstride, width2);
903

  
904
                    lefty= sub_left_prediction(s->temp[0], s->temp[0], width , lefty);
905
                    leftu= sub_left_prediction(s->temp[1], s->temp[1], width2, leftu);
906
                    leftv= sub_left_prediction(s->temp[2], s->temp[2], width2, leftv);
907
                }else{
908
                    lefty= sub_left_prediction(s->temp[0], ydst, width , lefty);
909
                    leftu= sub_left_prediction(s->temp[1], udst, width2, leftu);
910
                    leftv= sub_left_prediction(s->temp[2], vdst, width2, leftv);
911
                }
912

  
913
                encode_422_bitstream(s, width);
914
            }
915
        }        
916
    }else{
917
        fprintf(stderr, "Format not supported!\n");
918
    }
919
    emms_c();
920
    
921
    size= (get_bit_count(&s->pb)+31)/32;
922
    
923
    if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){
924
        int j;
925
        char *p= avctx->stats_out;
926
        for(i=0; i<3; i++){
927
            for(j=0; j<256; j++){
928
                sprintf(p, "%Ld ", s->stats[i][j]);
929
                p+= strlen(p);
930
                s->stats[i][j]= 0;
931
            }
932
            sprintf(p, "\n");
933
            p++;
934
        }
935
    }else{
936
        bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
937
    }
938
    
939
    avctx->key_frame= 1;
940
    avctx->pict_type= I_TYPE;
941
    
942
    s->picture_number++;
943
    
944
    return size*4;
945
}
946

  
947
static int encode_end(AVCodecContext *avctx)
948
{
949
//    HYuvContext *s = avctx->priv_data;
950

  
951
    av_freep(&avctx->extradata);
952
    av_freep(&avctx->stats_out);
953
    
954
    return 0;
955
}
956

  
957
AVCodec huffyuv_decoder = {
958
    "huffyuv",
959
    CODEC_TYPE_VIDEO,
960
    CODEC_ID_HUFFYUV,
961
    sizeof(HYuvContext),
962
    decode_init,
963
    NULL,
964
    decode_end,
965
    decode_frame,
966
    0,
967
    NULL
968
};
969

  
970
AVCodec huffyuv_encoder = {
971
    "huffyuv",
972
    CODEC_TYPE_VIDEO,
973
    CODEC_ID_HUFFYUV,
974
    sizeof(HYuvContext),
975
    encode_init,
976
    encode_frame,
977
    encode_end,
978
};
libavcodec/i386/dsputil_mmx.c
453 453
        return sum;
454 454
}
455 455

  
456
static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w){
457
    int i=0;
458
    asm volatile(
459
        "1:				\n\t"
460
        "movq  (%1, %0), %%mm0		\n\t"
461
        "movq  (%2, %0), %%mm1		\n\t"
462
        "paddb %%mm0, %%mm1		\n\t"
463
        "movq %%mm1, (%2, %0)		\n\t"
464
        "movq 8(%1, %0), %%mm0		\n\t"
465
        "movq 8(%2, %0), %%mm1		\n\t"
466
        "paddb %%mm0, %%mm1		\n\t"
467
        "movq %%mm1, 8(%2, %0)		\n\t"
468
        "addl $16, %0			\n\t"
469
        "cmpl %3, %0			\n\t"
470
        " jb 1b				\n\t"
471
        : "+r" (i)
472
        : "r"(src), "r"(dst), "r"(w-15)
473
    );
474
    for(; i<w; i++)
475
        dst[i+0] += src[i+0];
476
}
477

  
478
static void diff_bytes_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){
479
    int i=0;
480
    asm volatile(
481
        "1:				\n\t"
482
        "movq  (%2, %0), %%mm0		\n\t"
483
        "movq  (%1, %0), %%mm1		\n\t"
484
        "psubb %%mm0, %%mm1		\n\t"
485
        "movq %%mm1, (%3, %0)		\n\t"
486
        "movq 8(%2, %0), %%mm0		\n\t"
487
        "movq 8(%1, %0), %%mm1		\n\t"
488
        "psubb %%mm0, %%mm1		\n\t"
489
        "movq %%mm1, 8(%3, %0)		\n\t"
490
        "addl $16, %0			\n\t"
491
        "cmpl %4, %0			\n\t"
492
        " jb 1b				\n\t"
493
        : "+r" (i)
494
        : "r"(src1), "r"(src2), "r"(dst), "r"(w-15)
495
    );
496
    for(; i<w; i++)
497
        dst[i+0] = src1[i+0]-src2[i+0];
498
}
499

  
500

  
456 501
#if 0
457 502
static void just_return() { return; }
458 503
#endif
......
531 576
        c->avg_no_rnd_pixels_tab[1][1] = avg_no_rnd_pixels8_x2_mmx;
532 577
        c->avg_no_rnd_pixels_tab[1][2] = avg_no_rnd_pixels8_y2_mmx;
533 578
        c->avg_no_rnd_pixels_tab[1][3] = avg_no_rnd_pixels8_xy2_mmx;
579
        
580
        c->add_bytes= add_bytes_mmx;
581
        c->diff_bytes= diff_bytes_mmx;
534 582

  
535 583
        if (mm_flags & MM_MMXEXT) {
536 584
            c->pix_abs16x16     = pix_abs16x16_mmx2;

Also available in: Unified diff