Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master: (42 commits)
swscale: fix signed overflow in yuv2mono_X_c_template
snow: fix integer overflows
svq1enc: remove stale altivec-related hack
snow: fix signed overflow in byte to 32-bit replication
adx: rename ff_adx_decode_header() to avpriv_adx_decode_header()
avformat: add CRI ADX format demuxer
adx: add an ADX parser.
adx: move header decoding to ADX common code
adx: calculate the number of blocks in a packet
adx: define and use 2 new macro constants BLOCK_SIZE and BLOCK_SAMPLES
adx: check for unsupported ADX formats
adx: simplify encoding by using put_sbits()
adx: calculate correct LPC coeffs
adx: use 12-bit coefficients instead of 14-bit to avoid integer overflow
adx: simplify adx_decode() by using get_sbits() to read residual samples
adx: fix the data offset parsing in adx_decode_header()
adx: remove unneeded post-decode channel interleaving
adx: validate header values
adx: cosmetics: general pretty-printing and comment clean-up
adx: remove useless comments
...

Conflicts:
Changelog
libavcodec/cook.c
libavcodec/fraps.c
libavcodec/nuv.c
libavcodec/pthread.c
libavcodec/version.h
libavformat/Makefile
libavformat/version.h

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2011/11/27 08:30:13
Showing 34 changed files
... ...
@@ -125,6 +125,7 @@ easier to use. The changes are:
125 125
 - pan audio filter
126 126
 - IFF Amiga Continuous Bitmap (ACBM) decoder
127 127
 - ass filter
128
+- CRI ADX audio format demuxer
128 129
 
129 130
 
130 131
 version 0.8:
... ...
@@ -79,6 +79,8 @@ library:
79 79
 @item Brute Force & Ignorance   @tab   @tab X
80 80
     @tab Used in the game Flash Traffic: City of Angels.
81 81
 @item BWF                       @tab X @tab X
82
+@item CRI ADX                   @tab   @tab X
83
+    @tab Audio-only format used in console video games.
82 84
 @item Discworld II BMV          @tab   @tab X
83 85
 @item Interplay C93             @tab   @tab X
84 86
     @tab Used in the game Cyberia from Interplay.
... ...
@@ -519,7 +519,7 @@ OBJS-$(CONFIG_PCM_U32LE_ENCODER)          += pcm.o
519 519
 OBJS-$(CONFIG_PCM_ZORK_DECODER)           += pcm.o
520 520
 
521 521
 OBJS-$(CONFIG_ADPCM_4XM_DECODER)          += adpcm.o adpcm_data.o
522
-OBJS-$(CONFIG_ADPCM_ADX_DECODER)          += adxdec.o
522
+OBJS-$(CONFIG_ADPCM_ADX_DECODER)          += adxdec.o adx.o
523 523
 OBJS-$(CONFIG_ADPCM_ADX_ENCODER)          += adxenc.o
524 524
 OBJS-$(CONFIG_ADPCM_CT_DECODER)           += adpcm.o adpcm_data.o
525 525
 OBJS-$(CONFIG_ADPCM_EA_DECODER)           += adpcm.o
... ...
@@ -558,6 +558,7 @@ OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcmenc.o adpcm_data.o
558 558
 
559 559
 # libavformat dependencies
560 560
 OBJS-$(CONFIG_ADTS_MUXER)              += mpeg4audio.o
561
+OBJS-$(CONFIG_ADX_DEMUXER)             += adx.o
561 562
 OBJS-$(CONFIG_CAF_DEMUXER)             += mpeg4audio.o mpegaudiodata.o
562 563
 OBJS-$(CONFIG_DV_DEMUXER)              += dvdata.o
563 564
 OBJS-$(CONFIG_DV_MUXER)                += dvdata.o timecode.o
... ...
@@ -632,6 +633,7 @@ OBJS-$(CONFIG_AAC_PARSER)              += aac_parser.o aac_ac3_parser.o \
632 632
                                           aacadtsdec.o mpeg4audio.o
633 633
 OBJS-$(CONFIG_AC3_PARSER)              += ac3_parser.o ac3tab.o \
634 634
                                           aac_ac3_parser.o
635
+OBJS-$(CONFIG_ADX_PARSER)              += adx_parser.o adx.o
635 636
 OBJS-$(CONFIG_CAVSVIDEO_PARSER)        += cavs_parser.o
636 637
 OBJS-$(CONFIG_DCA_PARSER)              += dca_parser.o
637 638
 OBJS-$(CONFIG_DIRAC_PARSER)            += dirac_parser.o
638 639
new file mode 100644
... ...
@@ -0,0 +1,81 @@
0
+/*
1
+ * Copyright (c) 2011  Justin Ruggles
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/intreadwrite.h"
21
+#include "libavutil/mathematics.h"
22
+#include "adx.h"
23
+
24
+void ff_adx_calculate_coeffs(int cutoff, int sample_rate, int bits, int *coeff)
25
+{
26
+    double a, b, c;
27
+
28
+    a = M_SQRT2 - cos(2.0 * M_PI * cutoff / sample_rate);
29
+    b = M_SQRT2 - 1.0;
30
+    c = (a - sqrt((a + b) * (a - b))) / b;
31
+
32
+    coeff[0] = lrintf(c * 2.0  * (1 << bits));
33
+    coeff[1] = lrintf(-(c * c) * (1 << bits));
34
+}
35
+
36
+int avpriv_adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
37
+                             int bufsize, int *header_size, int *coeff)
38
+{
39
+    int offset, cutoff;
40
+
41
+    if (bufsize < 24)
42
+        return AVERROR_INVALIDDATA;
43
+
44
+    if (AV_RB16(buf) != 0x8000)
45
+        return AVERROR_INVALIDDATA;
46
+    offset = AV_RB16(buf + 2) + 4;
47
+
48
+    /* if copyright string is within the provided data, validate it */
49
+    if (bufsize >= offset && memcmp(buf + offset - 6, "(c)CRI", 6))
50
+        return AVERROR_INVALIDDATA;
51
+
52
+    /* check for encoding=3 block_size=18, sample_size=4 */
53
+    if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
54
+        av_log_ask_for_sample(avctx, "unsupported ADX format\n");
55
+        return AVERROR_PATCHWELCOME;
56
+    }
57
+
58
+    /* channels */
59
+    avctx->channels = buf[7];
60
+    if (avctx->channels > 2)
61
+        return AVERROR_INVALIDDATA;
62
+
63
+    /* sample rate */
64
+    avctx->sample_rate = AV_RB32(buf + 8);
65
+    if (avctx->sample_rate < 1 ||
66
+        avctx->sample_rate > INT_MAX / (avctx->channels * BLOCK_SIZE * 8))
67
+        return AVERROR_INVALIDDATA;
68
+
69
+    /* bit rate */
70
+    avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
71
+
72
+    /* LPC coefficients */
73
+    if (coeff) {
74
+        cutoff = AV_RB16(buf + 16);
75
+        ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff);
76
+    }
77
+
78
+    *header_size = offset;
79
+    return 0;
80
+}
... ...
@@ -31,19 +31,50 @@
31 31
 #ifndef AVCODEC_ADX_H
32 32
 #define AVCODEC_ADX_H
33 33
 
34
+#include <stdint.h>
35
+
36
+#include "avcodec.h"
37
+
34 38
 typedef struct {
35 39
     int s1,s2;
36
-} PREV;
40
+} ADXChannelState;
37 41
 
38 42
 typedef struct {
39
-    PREV prev[2];
43
+    int channels;
44
+    ADXChannelState prev[2];
40 45
     int header_parsed;
41
-    unsigned char dec_temp[18*2];
42
-    int in_temp;
46
+    int eof;
47
+    int cutoff;
48
+    int coeff[2];
43 49
 } ADXContext;
44 50
 
45
-#define    BASEVOL   0x4000
46
-#define    SCALE1    0x7298
47
-#define    SCALE2    0x3350
51
+#define COEFF_BITS  12
52
+
53
+#define BLOCK_SIZE      18
54
+#define BLOCK_SAMPLES   32
55
+
56
+/**
57
+ * Calculate LPC coefficients based on cutoff frequency and sample rate.
58
+ *
59
+ * @param cutoff       cutoff frequency
60
+ * @param sample_rate  sample rate
61
+ * @param bits         number of bits used to quantize coefficients
62
+ * @param[out] coeff   2 quantized LPC coefficients
63
+ */
64
+void ff_adx_calculate_coeffs(int cutoff, int sample_rate, int bits, int *coeff);
65
+
66
+/**
67
+ * Decode ADX stream header.
68
+ * Sets avctx->channels and avctx->sample_rate.
69
+ *
70
+ * @param      avctx        codec context
71
+ * @param      buf          header data
72
+ * @param      bufsize      data size, should be at least 24 bytes
73
+ * @param[out] header_size  size of ADX header
74
+ * @param[out] coeff        2 LPC coefficients, can be NULL
75
+ * @return data offset or negative error code if header is invalid
76
+ */
77
+int avpriv_adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
78
+                             int bufsize, int *header_size, int *coeff);
48 79
 
49 80
 #endif /* AVCODEC_ADX_H */
50 81
new file mode 100644
... ...
@@ -0,0 +1,104 @@
0
+/*
1
+ * Copyright (c) 2011  Justin Ruggles
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * ADX audio parser
23
+ *
24
+ * Reads header to extradata and splits packets into individual blocks.
25
+ */
26
+
27
+#include "libavutil/intreadwrite.h"
28
+#include "parser.h"
29
+#include "adx.h"
30
+
31
+typedef struct ADXParseContext {
32
+    ParseContext pc;
33
+    int header_size;
34
+    int block_size;
35
+    int buf_pos;
36
+} ADXParseContext;
37
+
38
+#define MIN_HEADER_SIZE 24
39
+
40
+static int adx_parse(AVCodecParserContext *s1,
41
+                           AVCodecContext *avctx,
42
+                           const uint8_t **poutbuf, int *poutbuf_size,
43
+                           const uint8_t *buf, int buf_size)
44
+{
45
+    ADXParseContext *s = s1->priv_data;
46
+    ParseContext *pc = &s->pc;
47
+    int next = END_NOT_FOUND;
48
+
49
+    if (!avctx->extradata_size) {
50
+        int ret;
51
+
52
+        ff_combine_frame(pc, END_NOT_FOUND, &buf, &buf_size);
53
+
54
+        if (!s->header_size && pc->index >= MIN_HEADER_SIZE) {
55
+            if (ret = avpriv_adx_decode_header(avctx, pc->buffer, pc->index,
56
+                                               &s->header_size, NULL))
57
+                return AVERROR_INVALIDDATA;
58
+            s->block_size = BLOCK_SIZE * avctx->channels;
59
+        }
60
+        if (s->header_size && s->header_size <= pc->index) {
61
+            avctx->extradata = av_mallocz(s->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
62
+            if (!avctx->extradata)
63
+                return AVERROR(ENOMEM);
64
+            avctx->extradata_size = s->header_size;
65
+            memcpy(avctx->extradata, pc->buffer, s->header_size);
66
+            memmove(pc->buffer, pc->buffer + s->header_size, s->header_size);
67
+            pc->index -= s->header_size;
68
+        }
69
+        *poutbuf      = NULL;
70
+        *poutbuf_size = 0;
71
+        return buf_size;
72
+    }
73
+
74
+    if (pc->index - s->buf_pos >= s->block_size) {
75
+        *poutbuf      = &pc->buffer[s->buf_pos];
76
+        *poutbuf_size = s->block_size;
77
+        s->buf_pos   += s->block_size;
78
+        return 0;
79
+    }
80
+    if (pc->index && s->buf_pos) {
81
+        memmove(pc->buffer, &pc->buffer[s->buf_pos], pc->index - s->buf_pos);
82
+        pc->index -= s->buf_pos;
83
+        s->buf_pos = 0;
84
+    }
85
+    if (buf_size + pc->index >= s->block_size)
86
+        next = s->block_size - pc->index;
87
+
88
+    if (ff_combine_frame(pc, next, &buf, &buf_size) < 0 || !buf_size) {
89
+        *poutbuf      = NULL;
90
+        *poutbuf_size = 0;
91
+        return buf_size;
92
+    }
93
+    *poutbuf = buf;
94
+    *poutbuf_size = buf_size;
95
+    return next;
96
+}
97
+
98
+AVCodecParser ff_adx_parser = {
99
+    .codec_ids      = { CODEC_ID_ADPCM_ADX },
100
+    .priv_data_size = sizeof(ADXParseContext),
101
+    .parser_parse   = adx_parse,
102
+    .parser_close   = ff_parse_close,
103
+};
... ...
@@ -22,6 +22,7 @@
22 22
 #include "libavutil/intreadwrite.h"
23 23
 #include "avcodec.h"
24 24
 #include "adx.h"
25
+#include "get_bits.h"
25 26
 
26 27
 /**
27 28
  * @file
... ...
@@ -34,136 +35,105 @@
34 34
 
35 35
 static av_cold int adx_decode_init(AVCodecContext *avctx)
36 36
 {
37
+    ADXContext *c = avctx->priv_data;
38
+    int ret, header_size;
39
+
40
+    if (avctx->extradata_size < 24)
41
+        return AVERROR_INVALIDDATA;
42
+
43
+    if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
44
+                                        avctx->extradata_size, &header_size,
45
+                                        c->coeff)) < 0) {
46
+        av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
47
+        return AVERROR_INVALIDDATA;
48
+    }
49
+    c->channels = avctx->channels;
50
+
37 51
     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
38 52
     return 0;
39 53
 }
40 54
 
41
-/* 18 bytes <-> 32 samples */
42
-
43
-static void adx_decode(short *out,const unsigned char *in,PREV *prev)
55
+/**
56
+ * Decode 32 samples from 18 bytes.
57
+ *
58
+ * A 16-bit scalar value is applied to 32 residuals, which then have a
59
+ * 2nd-order LPC filter applied to it to form the output signal for a single
60
+ * channel.
61
+ */
62
+static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
44 63
 {
64
+    ADXChannelState *prev = &c->prev[ch];
65
+    GetBitContext gb;
45 66
     int scale = AV_RB16(in);
46 67
     int i;
47
-    int s0,s1,s2,d;
68
+    int s0, s1, s2, d;
48 69
 
49
-//    printf("%x ",scale);
70
+    /* check if this is an EOF packet */
71
+    if (scale & 0x8000)
72
+        return -1;
50 73
 
51
-    in+=2;
74
+    init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
52 75
     s1 = prev->s1;
53 76
     s2 = prev->s2;
54
-    for(i=0;i<16;i++) {
55
-        d = in[i];
56
-        // d>>=4; if (d&8) d-=16;
57
-        d = ((signed char)d >> 4);
58
-        s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
59
-        s2 = s1;
60
-        s1 = av_clip_int16(s0);
61
-        *out++=s1;
62
-
63
-        d = in[i];
64
-        //d&=15; if (d&8) d-=16;
65
-        d = ((signed char)(d<<4) >> 4);
66
-        s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
77
+    for (i = 0; i < BLOCK_SAMPLES; i++) {
78
+        d  = get_sbits(&gb, 4);
79
+        s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
67 80
         s2 = s1;
68 81
         s1 = av_clip_int16(s0);
69
-        *out++=s1;
82
+        *out = s1;
83
+        out += c->channels;
70 84
     }
71 85
     prev->s1 = s1;
72 86
     prev->s2 = s2;
73 87
 
88
+    return 0;
74 89
 }
75 90
 
76
-static void adx_decode_stereo(short *out,const unsigned char *in,PREV *prev)
77
-{
78
-    short tmp[32*2];
79
-    int i;
80
-
81
-    adx_decode(tmp   ,in   ,prev);
82
-    adx_decode(tmp+32,in+18,prev+1);
83
-    for(i=0;i<32;i++) {
84
-        out[i*2]   = tmp[i];
85
-        out[i*2+1] = tmp[i+32];
86
-    }
87
-}
88
-
89
-/* return data offset or 0 */
90
-static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize)
91
-{
92
-    int offset;
93
-
94
-    if (buf[0]!=0x80) return 0;
95
-    offset = (AV_RB32(buf)^0x80000000)+4;
96
-    if (bufsize<offset || memcmp(buf+offset-6,"(c)CRI",6)) return 0;
97
-
98
-    avctx->channels    = buf[7];
99
-    avctx->sample_rate = AV_RB32(buf+8);
100
-    avctx->bit_rate    = avctx->sample_rate*avctx->channels*18*8/32;
101
-
102
-    return offset;
103
-}
104
-
105
-static int adx_decode_frame(AVCodecContext *avctx,
106
-                void *data, int *data_size,
107
-                AVPacket *avpkt)
91
+static int adx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
92
+                            AVPacket *avpkt)
108 93
 {
109
-    const uint8_t *buf0 = avpkt->data;
110
-    int buf_size = avpkt->size;
111
-    ADXContext *c = avctx->priv_data;
112
-    short *samples = data;
113
-    const uint8_t *buf = buf0;
114
-    int rest = buf_size;
115
-
116
-    if (!c->header_parsed) {
117
-        int hdrsize = adx_decode_header(avctx,buf,rest);
118
-        if (hdrsize==0) return -1;
119
-        c->header_parsed = 1;
120
-        buf  += hdrsize;
121
-        rest -= hdrsize;
94
+    int buf_size        = avpkt->size;
95
+    ADXContext *c       = avctx->priv_data;
96
+    int16_t *samples    = data;
97
+    const uint8_t *buf  = avpkt->data;
98
+    int num_blocks, ch;
99
+
100
+    if (c->eof) {
101
+        *data_size = 0;
102
+        return buf_size;
122 103
     }
123 104
 
124 105
     /* 18 bytes of data are expanded into 32*2 bytes of audio,
125 106
        so guard against buffer overflows */
126
-    if(rest/18 > *data_size/64)
127
-        rest = (*data_size/64) * 18;
128
-
129
-    if (c->in_temp) {
130
-        int copysize = 18*avctx->channels - c->in_temp;
131
-        memcpy(c->dec_temp+c->in_temp,buf,copysize);
132
-        rest -= copysize;
133
-        buf  += copysize;
134
-        if (avctx->channels==1) {
135
-            adx_decode(samples,c->dec_temp,c->prev);
136
-            samples += 32;
137
-        } else {
138
-            adx_decode_stereo(samples,c->dec_temp,c->prev);
139
-            samples += 32*2;
140
-        }
107
+    num_blocks = buf_size / (BLOCK_SIZE * c->channels);
108
+    if (num_blocks > *data_size / (BLOCK_SAMPLES * c->channels)) {
109
+        buf_size   = (*data_size / (BLOCK_SAMPLES * c->channels)) * BLOCK_SIZE;
110
+        num_blocks = buf_size / (BLOCK_SIZE * c->channels);
141 111
     }
142
-    //
143
-    if (avctx->channels==1) {
144
-        while(rest>=18) {
145
-            adx_decode(samples,buf,c->prev);
146
-            rest-=18;
147
-            buf+=18;
148
-            samples+=32;
149
-        }
150
-    } else {
151
-        while(rest>=18*2) {
152
-            adx_decode_stereo(samples,buf,c->prev);
153
-            rest-=18*2;
154
-            buf+=18*2;
155
-            samples+=32*2;
112
+    if (!buf_size || buf_size % (BLOCK_SIZE * avctx->channels)) {
113
+        if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
114
+            c->eof = 1;
115
+            *data_size = 0;
116
+            return avpkt->size;
156 117
         }
118
+        return AVERROR_INVALIDDATA;
157 119
     }
158
-    //
159
-    c->in_temp = rest;
160
-    if (rest) {
161
-        memcpy(c->dec_temp,buf,rest);
162
-        buf+=rest;
120
+
121
+    while (num_blocks--) {
122
+        for (ch = 0; ch < c->channels; ch++) {
123
+            if (adx_decode(c, samples + ch, buf, ch)) {
124
+                c->eof = 1;
125
+                buf = avpkt->data + avpkt->size;
126
+                break;
127
+            }
128
+            buf_size -= BLOCK_SIZE;
129
+            buf      += BLOCK_SIZE;
130
+        }
131
+        samples += BLOCK_SAMPLES * c->channels;
163 132
     }
133
+
164 134
     *data_size = (uint8_t*)samples - (uint8_t*)data;
165
-//    printf("%d:%d ",buf-buf0,*data_size); fflush(stdout);
166
-    return buf-buf0;
135
+    return buf - avpkt->data;
167 136
 }
168 137
 
169 138
 AVCodec ff_adpcm_adx_decoder = {
... ...
@@ -173,6 +143,5 @@ AVCodec ff_adpcm_adx_decoder = {
173 173
     .priv_data_size = sizeof(ADXContext),
174 174
     .init           = adx_decode_init,
175 175
     .decode         = adx_decode_frame,
176
-    .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
176
+    .long_name      = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
177 177
 };
178
-
... ...
@@ -22,6 +22,7 @@
22 22
 #include "libavutil/intreadwrite.h"
23 23
 #include "avcodec.h"
24 24
 #include "adx.h"
25
+#include "put_bits.h"
25 26
 
26 27
 /**
27 28
  * @file
... ...
@@ -34,8 +35,10 @@
34 34
 
35 35
 /* 18 bytes <-> 32 samples */
36 36
 
37
-static void adx_encode(unsigned char *adx,const short *wav,PREV *prev)
37
+static void adx_encode(ADXContext *c, unsigned char *adx, const short *wav,
38
+                       ADXChannelState *prev)
38 39
 {
40
+    PutBitContext pb;
39 41
     int scale;
40 42
     int i;
41 43
     int s0,s1,s2,d;
... ...
@@ -47,7 +50,7 @@ static void adx_encode(unsigned char *adx,const short *wav,PREV *prev)
47 47
     s2 = prev->s2;
48 48
     for(i=0;i<32;i++) {
49 49
         s0 = wav[i];
50
-        d = ((s0<<14) - SCALE1*s1 + SCALE2*s2)/BASEVOL;
50
+        d = ((s0 << COEFF_BITS) - c->coeff[0] * s1 - c->coeff[1] * s2) >> COEFF_BITS;
51 51
         data[i]=d;
52 52
         if (max<d) max=d;
53 53
         if (min>d) min=d;
... ...
@@ -71,9 +74,10 @@ static void adx_encode(unsigned char *adx,const short *wav,PREV *prev)
71 71
 
72 72
     AV_WB16(adx, scale);
73 73
 
74
-    for(i=0;i<16;i++) {
75
-        adx[i+2] = ((data[i*2]/scale)<<4) | ((data[i*2+1]/scale)&0xf);
76
-    }
74
+    init_put_bits(&pb, adx + 2, 16);
75
+    for (i = 0; i < 32; i++)
76
+        put_sbits(&pb, 4, av_clip(data[i]/scale, -8, 7));
77
+    flush_put_bits(&pb);
77 78
 }
78 79
 
79 80
 static int adx_encode_header(AVCodecContext *avctx,unsigned char *buf,size_t bufsize)
... ...
@@ -101,19 +105,24 @@ static int adx_encode_header(AVCodecContext *avctx,unsigned char *buf,size_t buf
101 101
     } adxhdr; /* big endian */
102 102
     /* offset-6 "(c)CRI" */
103 103
 #endif
104
+    ADXContext *c = avctx->priv_data;
105
+
104 106
     AV_WB32(buf+0x00,0x80000000|0x20);
105 107
     AV_WB32(buf+0x04,0x03120400|avctx->channels);
106 108
     AV_WB32(buf+0x08,avctx->sample_rate);
107 109
     AV_WB32(buf+0x0c,0); /* FIXME: set after */
108
-    AV_WB32(buf+0x10,0x01040300);
109
-    AV_WB32(buf+0x14,0x00000000);
110
-    AV_WB32(buf+0x18,0x00000000);
111
-    memcpy(buf+0x1c,"\0\0(c)CRI",8);
110
+    AV_WB16(buf + 0x10, c->cutoff);
111
+    AV_WB32(buf + 0x12, 0x03000000);
112
+    AV_WB32(buf + 0x16, 0x00000000);
113
+    AV_WB32(buf + 0x1a, 0x00000000);
114
+    memcpy (buf + 0x1e, "(c)CRI", 6);
112 115
     return 0x20+4;
113 116
 }
114 117
 
115 118
 static av_cold int adx_encode_init(AVCodecContext *avctx)
116 119
 {
120
+    ADXContext *c = avctx->priv_data;
121
+
117 122
     if (avctx->channels > 2)
118 123
         return -1; /* only stereo or mono =) */
119 124
     avctx->frame_size = 32;
... ...
@@ -123,6 +132,10 @@ static av_cold int adx_encode_init(AVCodecContext *avctx)
123 123
 
124 124
 //    avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
125 125
 
126
+    /* the cutoff can be adjusted, but this seems to work pretty well */
127
+    c->cutoff = 500;
128
+    ff_adx_calculate_coeffs(c->cutoff, avctx->sample_rate, COEFF_BITS, c->coeff);
129
+
126 130
     av_log(avctx, AV_LOG_DEBUG, "adx encode init\n");
127 131
 
128 132
     return 0;
... ...
@@ -158,7 +171,7 @@ static int adx_encode_frame(AVCodecContext *avctx,
158 158
 
159 159
     if (avctx->channels==1) {
160 160
         while(rest>=32) {
161
-            adx_encode(dst,samples,c->prev);
161
+            adx_encode(c, dst, samples, c->prev);
162 162
             dst+=18;
163 163
             samples+=32;
164 164
             rest-=32;
... ...
@@ -173,8 +186,8 @@ static int adx_encode_frame(AVCodecContext *avctx,
173 173
                 tmpbuf[i+32] = samples[i*2+1];
174 174
             }
175 175
 
176
-            adx_encode(dst,tmpbuf,c->prev);
177
-            adx_encode(dst+18,tmpbuf+32,c->prev+1);
176
+            adx_encode(c, dst,      tmpbuf,      c->prev);
177
+            adx_encode(c, dst + 18, tmpbuf + 32, c->prev + 1);
178 178
             dst+=18*2;
179 179
             samples+=32*2;
180 180
             rest-=32*2;
... ...
@@ -414,6 +414,7 @@ void avcodec_register_all(void)
414 414
     REGISTER_PARSER  (AAC, aac);
415 415
     REGISTER_PARSER  (AAC_LATM, aac_latm);
416 416
     REGISTER_PARSER  (AC3, ac3);
417
+    REGISTER_PARSER  (ADX, adx);
417 418
     REGISTER_PARSER  (CAVSVIDEO, cavsvideo);
418 419
     REGISTER_PARSER  (DCA, dca);
419 420
     REGISTER_PARSER  (DIRAC, dirac);
... ...
@@ -690,7 +690,7 @@ static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,
690 690
             /* Update the adaption coefficients */
691 691
             absres = FFABS(res);
692 692
             if (absres)
693
-                *f->adaptcoeffs = ((res & (1<<31)) - (1<<30)) >>
693
+                *f->adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
694 694
                                   (25 + (absres <= f->avg*3) + (absres <= f->avg*4/3));
695 695
             else
696 696
                 *f->adaptcoeffs = 0;
... ...
@@ -1259,7 +1259,7 @@ struct AVCodecInternal;
1259 1259
 typedef struct AVCodecContext {
1260 1260
     /**
1261 1261
      * information on struct for av_log
1262
-     * - set by avcodec_alloc_context
1262
+     * - set by avcodec_alloc_context3
1263 1263
      */
1264 1264
     const AVClass *av_class;
1265 1265
     /**
... ...
@@ -2959,8 +2959,8 @@ typedef struct AVCodecContext {
2959 2959
      * For SUBTITLE_ASS subtitle type, it should contain the whole ASS
2960 2960
      * [Script Info] and [V4+ Styles] section, plus the [Events] line and
2961 2961
      * the Format line following. It shouldn't include any Dialogue line.
2962
-     * - encoding: Set/allocated/freed by user (before avcodec_open())
2963
-     * - decoding: Set/allocated/freed by libavcodec (by avcodec_open())
2962
+     * - encoding: Set/allocated/freed by user (before avcodec_open2())
2963
+     * - decoding: Set/allocated/freed by libavcodec (by avcodec_open2())
2964 2964
      */
2965 2965
     uint8_t *subtitle_header;
2966 2966
     int subtitle_header_size;
... ...
@@ -3853,7 +3853,7 @@ AVCodecContext *avcodec_alloc_context2(enum AVMediaType);
3853 3853
  * resulting struct can be deallocated by simply calling av_free().
3854 3854
  *
3855 3855
  * @param codec if non-NULL, allocate private data and initialize defaults
3856
- *              for the given codec. It is illegal to then call avcodec_open()
3856
+ *              for the given codec. It is illegal to then call avcodec_open2()
3857 3857
  *              with a different codec.
3858 3858
  *
3859 3859
  * @return An AVCodecContext filled with default values or NULL on failure.
... ...
@@ -3864,7 +3864,7 @@ AVCodecContext *avcodec_alloc_context3(AVCodec *codec);
3864 3864
 /**
3865 3865
  * Copy the settings of the source AVCodecContext into the destination
3866 3866
  * AVCodecContext. The resulting destination codec context will be
3867
- * unopened, i.e. you are required to call avcodec_open() before you
3867
+ * unopened, i.e. you are required to call avcodec_open2() before you
3868 3868
  * can use this AVCodecContext to decode/encode video/audio data.
3869 3869
  *
3870 3870
  * @param dest target codec context, should be initialized with
... ...
@@ -3928,7 +3928,7 @@ enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum
3928 3928
 
3929 3929
 #if FF_API_THREAD_INIT
3930 3930
 /**
3931
- * @deprecated Set s->thread_count before calling avcodec_open() instead of calling this.
3931
+ * @deprecated Set s->thread_count before calling avcodec_open2() instead of calling this.
3932 3932
  */
3933 3933
 attribute_deprecated
3934 3934
 int avcodec_thread_init(AVCodecContext *s, int thread_count);
... ...
@@ -3974,7 +3974,7 @@ int avcodec_open(AVCodecContext *avctx, AVCodec *codec);
3974 3974
 
3975 3975
 /**
3976 3976
  * Initialize the AVCodecContext to use the given AVCodec. Prior to using this
3977
- * function the context has to be allocated with avcodec_alloc_context().
3977
+ * function the context has to be allocated with avcodec_alloc_context3().
3978 3978
  *
3979 3979
  * The functions avcodec_find_decoder_by_name(), avcodec_find_encoder_by_name(),
3980 3980
  * avcodec_find_decoder() and avcodec_find_encoder() provide an easy way for
... ...
@@ -3989,9 +3989,9 @@ int avcodec_open(AVCodecContext *avctx, AVCodec *codec);
3989 3989
  * if (!codec)
3990 3990
  *     exit(1);
3991 3991
  *
3992
- * context = avcodec_alloc_context();
3992
+ * context = avcodec_alloc_context3(codec);
3993 3993
  *
3994
- * if (avcodec_open(context, codec, opts) < 0)
3994
+ * if (avcodec_open2(context, codec, opts) < 0)
3995 3995
  *     exit(1);
3996 3996
  * @endcode
3997 3997
  *
... ...
@@ -273,6 +273,10 @@ static av_cold void init_cplscales_table (COOKContext *q) {
273 273
  */
274 274
 
275 275
 static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
276
+    static const uint32_t tab[4] = {
277
+        AV_BE2NE32C(0x37c511f2), AV_BE2NE32C(0xf237c511),
278
+        AV_BE2NE32C(0x11f237c5), AV_BE2NE32C(0xc511f237),
279
+    };
276 280
     int i, off;
277 281
     uint32_t c;
278 282
     const uint32_t* buf;
... ...
@@ -285,7 +289,7 @@ static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes)
285 285
 
286 286
     off = (intptr_t)inbuffer & 3;
287 287
     buf = (const uint32_t*) (inbuffer - off);
288
-    c = av_be2ne32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));
288
+    c = tab[off];
289 289
     bytes += 3 + off;
290 290
     for (i = 0; i < bytes/4; i++)
291 291
         obuf[i] = c ^ buf[i];
... ...
@@ -1075,7 +1079,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1075 1075
             q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr);
1076 1076
             extradata_size -= 8;
1077 1077
         }
1078
-        if (extradata_size >= 8){
1078
+        if (extradata_size >= 8) {
1079 1079
             bytestream_get_be32(&edata_ptr);    //Unknown unused
1080 1080
             q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr);
1081 1081
             q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr);
... ...
@@ -680,7 +680,8 @@ static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
680 680
                 int qscale = 1;
681 681
                 int mb = y*ctx->m.mb_width+x;
682 682
                 for (q = 1; q < avctx->qmax; q++) {
683
-                    unsigned score = ctx->mb_rc[q][mb].bits*lambda+(ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);
683
+                    unsigned score = ctx->mb_rc[q][mb].bits*lambda+
684
+                        ((unsigned)ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);
684 685
                     if (score < min) {
685 686
                         min = score;
686 687
                         qscale = q;
... ...
@@ -707,7 +708,7 @@ static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
707 707
                 lambda = (lambda+last_higher)>>1;
708 708
             else
709 709
                 lambda -= down_step;
710
-            down_step *= 5; // XXX tune ?
710
+            down_step = FFMIN((int64_t)down_step*5, INT_MAX);
711 711
             up_step = 1<<LAMBDA_FRAC_BITS;
712 712
             lambda = FFMAX(1, lambda);
713 713
             if (lambda == last_lower)
... ...
@@ -364,7 +364,7 @@ typedef struct BlockInfo {
364 364
     uint8_t pos; /* position in block */
365 365
     void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
366 366
     uint8_t partial_bit_count;
367
-    uint16_t partial_bit_buffer;
367
+    uint32_t partial_bit_buffer;
368 368
     int shift_offset;
369 369
 } BlockInfo;
370 370
 
... ...
@@ -392,8 +392,7 @@ static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
392 392
 
393 393
     /* if we must parse a partial VLC, we do it here */
394 394
     if (partial_bit_count > 0) {
395
-        re_cache = ((unsigned)re_cache >> partial_bit_count) |
396
-                   (mb->partial_bit_buffer << (sizeof(re_cache) * 8 - partial_bit_count));
395
+        re_cache = re_cache >> partial_bit_count | mb->partial_bit_buffer;
397 396
         re_index -= partial_bit_count;
398 397
         mb->partial_bit_count = 0;
399 398
     }
... ...
@@ -416,7 +415,7 @@ static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
416 416
         if (re_index + vlc_len > last_index) {
417 417
             /* should be < 16 bits otherwise a codeword could have been parsed */
418 418
             mb->partial_bit_count = last_index - re_index;
419
-            mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count);
419
+            mb->partial_bit_buffer = re_cache & ~(-1u >> mb->partial_bit_count);
420 420
             re_index = last_index;
421 421
             break;
422 422
         }
... ...
@@ -112,9 +112,9 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
112 112
              */
113 113
             if(j) dst[i] += dst[i - stride];
114 114
             else if(Uoff) dst[i] += 0x80;
115
-            if(get_bits_left(&gb) < 0){
115
+            if (get_bits_left(&gb) < 0) {
116 116
                 free_vlc(&vlc);
117
-                return -1;
117
+                return AVERROR_INVALIDDATA;
118 118
             }
119 119
         }
120 120
         dst += stride;
... ...
@@ -96,11 +96,11 @@ static int gif_read_image(GifState *s)
96 96
     n = (1 << bits_per_pixel);
97 97
     spal = palette;
98 98
     for(i = 0; i < n; i++) {
99
-        s->image_palette[i] = (0xff << 24) | AV_RB24(spal);
99
+        s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
100 100
         spal += 3;
101 101
     }
102 102
     for(; i < 256; i++)
103
-        s->image_palette[i] = (0xff << 24);
103
+        s->image_palette[i] = (0xffu << 24);
104 104
     /* handle transparency */
105 105
     if (s->transparent_color_index >= 0)
106 106
         s->image_palette[s->transparent_color_index] = 0;
... ...
@@ -193,7 +193,7 @@ retry:
193 193
         int w, h, q, res;
194 194
         if (buf[0] != 'V' || buf_size < 12) {
195 195
             av_log(avctx, AV_LOG_ERROR, "invalid nuv video frame (wrong codec_tag?)\n");
196
-            return -1;
196
+            return AVERROR_INVALIDDATA;
197 197
         }
198 198
         w = AV_RL16(&buf[6]);
199 199
         h = AV_RL16(&buf[8]);
... ...
@@ -658,7 +658,7 @@ static void frame_thread_free(AVCodecContext *avctx, int thread_count)
658 658
         pthread_cond_signal(&p->input_cond);
659 659
         pthread_mutex_unlock(&p->mutex);
660 660
 
661
-        if(p->thread)
661
+        if (p->thread)
662 662
             pthread_join(p->thread, NULL);
663 663
 
664 664
         if (codec->close)
... ...
@@ -332,7 +332,6 @@ static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int
332 332
     int rle_code;
333 333
     int pixel_ptr;
334 334
     int row_inc = s->frame.linesize[0];
335
-    unsigned char a, r, g, b;
336 335
     unsigned int argb;
337 336
     unsigned char *rgb = s->frame.data[0];
338 337
     int pixel_limit = s->frame.linesize[0] * s->avctx->height;
... ...
@@ -352,16 +351,13 @@ static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int
352 352
                 /* decode the run length code */
353 353
                 rle_code = -rle_code;
354 354
                 CHECK_STREAM_PTR(4);
355
-                a = s->buf[stream_ptr++];
356
-                r = s->buf[stream_ptr++];
357
-                g = s->buf[stream_ptr++];
358
-                b = s->buf[stream_ptr++];
359
-                argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
355
+                argb = AV_RB32(s->buf + stream_ptr);
356
+                stream_ptr += 4;
360 357
 
361 358
                 CHECK_PIXEL_PTR(rle_code * 4);
362 359
 
363 360
                 while (rle_code--) {
364
-                    *(unsigned int *)(&rgb[pixel_ptr]) = argb;
361
+                    AV_WN32A(rgb + pixel_ptr, argb);
365 362
                     pixel_ptr += 4;
366 363
                 }
367 364
             } else {
... ...
@@ -370,13 +366,10 @@ static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int
370 370
 
371 371
                 /* copy pixels directly to output */
372 372
                 while (rle_code--) {
373
-                    a = s->buf[stream_ptr++];
374
-                    r = s->buf[stream_ptr++];
375
-                    g = s->buf[stream_ptr++];
376
-                    b = s->buf[stream_ptr++];
377
-                    argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
378
-                    *(unsigned int *)(&rgb[pixel_ptr]) = argb;
379
-                    pixel_ptr += 4;
373
+                    argb = AV_RB32(s->buf + stream_ptr);
374
+                    AV_WN32A(rgb + pixel_ptr, argb);
375
+                    stream_ptr += 4;
376
+                    pixel_ptr  += 4;
380 377
                 }
381 378
             }
382 379
         }
... ...
@@ -933,7 +933,7 @@ static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int
933 933
         if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
934 934
     }
935 935
     if(!right && up){
936
-        topleft = dst[-stride + 3] * 0x01010101;
936
+        topleft = dst[-stride + 3] * 0x01010101u;
937 937
         prev = (uint8_t*)&topleft;
938 938
     }
939 939
     r->h.pred4x4[itype](dst, prev, stride);
... ...
@@ -28,7 +28,7 @@
28 28
 static inline void dxt1_decode_pixels(const uint8_t *s, uint32_t *d,
29 29
                                       unsigned int qstride, unsigned int flag,
30 30
                                       uint64_t alpha) {
31
-    unsigned int x, y, c0, c1, a = (!flag * 255) << 24;
31
+    unsigned int x, y, c0, c1, a = (!flag * 255u) << 24;
32 32
     unsigned int rb0, rb1, rb2, rb3, g0, g1, g2, g3;
33 33
     uint32_t colors[4], pixels;
34 34
 
... ...
@@ -290,8 +290,8 @@ static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, int stride, int
290 290
 void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
291 291
     if(block->type & BLOCK_INTRA){
292 292
         int x, y;
293
-        const int color = block->color[plane_index];
294
-        const int color4= color*0x01010101;
293
+        const unsigned color  = block->color[plane_index];
294
+        const unsigned color4 = color*0x01010101;
295 295
         if(b_w==32){
296 296
             for(y=0; y < b_h; y++){
297 297
                 *(uint32_t*)&dst[0 + y*stride]= color4;
... ...
@@ -154,8 +154,8 @@ typedef struct SnowContext{
154 154
     Plane plane[MAX_PLANES];
155 155
     BlockNode *block;
156 156
 #define ME_CACHE_SIZE 1024
157
-    int me_cache[ME_CACHE_SIZE];
158
-    int me_cache_generation;
157
+    unsigned me_cache[ME_CACHE_SIZE];
158
+    unsigned me_cache_generation;
159 159
     slice_buffer sb;
160 160
     int memc_only;
161 161
 
... ...
@@ -958,7 +958,8 @@ static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int
958 958
     const int b_stride= s->b_width << s->block_max_depth;
959 959
     BlockNode *block= &s->block[mb_x + mb_y * b_stride];
960 960
     BlockNode backup= *block;
961
-    int rd, index, value;
961
+    unsigned value;
962
+    int rd, index;
962 963
 
963 964
     assert(mb_x>=0 && mb_y>=0);
964 965
     assert(mb_x<b_stride);
... ...
@@ -1003,7 +1004,8 @@ static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_
1003 1003
     const int b_stride= s->b_width << s->block_max_depth;
1004 1004
     BlockNode *block= &s->block[mb_x + mb_y * b_stride];
1005 1005
     BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
1006
-    int rd, index, value;
1006
+    unsigned value;
1007
+    int rd, index;
1007 1008
 
1008 1009
     assert(mb_x>=0 && mb_y>=0);
1009 1010
     assert(mb_x<b_stride);
... ...
@@ -317,9 +317,9 @@ static int svq1_decode_motion_vector (GetBitContext *bitbuf, svq1_pmv *mv, svq1_
317 317
 
318 318
     /* add median of motion vector predictors and clip result */
319 319
     if (i == 1)
320
-      mv->y = ((diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y)) << 26) >> 26;
320
+      mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
321 321
     else
322
-      mv->x = ((diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x)) << 26) >> 26;
322
+      mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
323 323
   }
324 324
 
325 325
   return 0;
... ...
@@ -113,10 +113,6 @@ static void svq1_write_header(SVQ1Context *s, int frame_type)
113 113
 #define QUALITY_THRESHOLD 100
114 114
 #define THRESHOLD_MULTIPLIER 0.6
115 115
 
116
-#if HAVE_ALTIVEC
117
-#undef vector
118
-#endif
119
-
120 116
 static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, int level, int threshold, int lambda, int intra){
121 117
     int count, y, x, i, j, split, best_mean, best_score, best_count;
122 118
     int best_vector[6];
... ...
@@ -160,7 +156,7 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref, uint8_t *dec
160 160
     }
161 161
 
162 162
     best_count=0;
163
-    best_score -= ((block_sum[0]*block_sum[0])>>(level+3));
163
+    best_score -= (int)(((unsigned)block_sum[0]*block_sum[0])>>(level+3));
164 164
     best_mean= (block_sum[0] + (size>>1)) >> (level+3);
165 165
 
166 166
     if(level<4){
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR 37
24
+#define LIBAVCODEC_VERSION_MINOR 38
25 25
 #define LIBAVCODEC_VERSION_MICRO  1
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -25,6 +25,7 @@ OBJS-$(CONFIG_AC3_DEMUXER)               += ac3dec.o rawdec.o
25 25
 OBJS-$(CONFIG_AC3_MUXER)                 += rawenc.o
26 26
 OBJS-$(CONFIG_ACT_DEMUXER)               += act.o
27 27
 OBJS-$(CONFIG_ADF_DEMUXER)               += bintext.o sauce.o
28
+OBJS-$(CONFIG_ADX_DEMUXER)               += adxdec.o
28 29
 OBJS-$(CONFIG_ADTS_MUXER)                += adtsenc.o
29 30
 OBJS-$(CONFIG_AEA_DEMUXER)               += aea.o pcm.o
30 31
 OBJS-$(CONFIG_AIFF_DEMUXER)              += aiffdec.o riff.o pcm.o isom.o
31 32
new file mode 100644
... ...
@@ -0,0 +1,111 @@
0
+/*
1
+ * Copyright (c) 2011 Justin Ruggles
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * CRI ADX demuxer
23
+ */
24
+
25
+#include "libavutil/intreadwrite.h"
26
+#include "libavcodec/adx.h"
27
+#include "avformat.h"
28
+
29
+#define BLOCK_SIZE    18
30
+#define BLOCK_SAMPLES 32
31
+
32
+typedef struct ADXDemuxerContext {
33
+    int header_size;
34
+} ADXDemuxerContext;
35
+
36
+static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
37
+{
38
+    ADXDemuxerContext *c = s->priv_data;
39
+    AVCodecContext *avctx = s->streams[0]->codec;
40
+    int ret, size;
41
+
42
+    size = BLOCK_SIZE * avctx->channels;
43
+
44
+    pkt->pos = avio_tell(s->pb);
45
+    pkt->stream_index = 0;
46
+
47
+    ret = av_get_packet(s->pb, pkt, size);
48
+    if (ret != size) {
49
+        av_free_packet(pkt);
50
+        return ret < 0 ? ret : AVERROR(EIO);
51
+    }
52
+    if (AV_RB16(pkt->data) & 0x8000) {
53
+        av_free_packet(pkt);
54
+        return AVERROR_EOF;
55
+    }
56
+    pkt->size     = size;
57
+    pkt->duration = 1;
58
+    pkt->pts      = (pkt->pos - c->header_size) / size;
59
+
60
+    return 0;
61
+}
62
+
63
+static int adx_read_header(AVFormatContext *s, AVFormatParameters *ap)
64
+{
65
+    ADXDemuxerContext *c = s->priv_data;
66
+    AVCodecContext *avctx;
67
+    int ret;
68
+
69
+    AVStream *st = avformat_new_stream(s, NULL);
70
+    if (!st)
71
+        return AVERROR(ENOMEM);
72
+    avctx = s->streams[0]->codec;
73
+
74
+    if (avio_rb16(s->pb) != 0x8000)
75
+        return AVERROR_INVALIDDATA;
76
+    c->header_size = avio_rb16(s->pb) + 4;
77
+    avio_seek(s->pb, -4, SEEK_CUR);
78
+
79
+    avctx->extradata = av_mallocz(c->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
80
+    if (!avctx->extradata)
81
+        return AVERROR(ENOMEM);
82
+    if (avio_read(s->pb, avctx->extradata, c->header_size) < c->header_size) {
83
+        av_freep(&avctx->extradata);
84
+        return AVERROR(EIO);
85
+    }
86
+    avctx->extradata_size = c->header_size;
87
+
88
+    ret = avpriv_adx_decode_header(avctx, avctx->extradata,
89
+                                   avctx->extradata_size, &c->header_size,
90
+                                   NULL);
91
+    if (ret)
92
+        return ret;
93
+
94
+    st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
95
+    st->codec->codec_id    = s->iformat->value;
96
+
97
+    av_set_pts_info(st, 64, BLOCK_SAMPLES, avctx->sample_rate);
98
+
99
+    return 0;
100
+}
101
+
102
+AVInputFormat ff_adx_demuxer = {
103
+    .name           = "adx",
104
+    .long_name      = NULL_IF_CONFIG_SMALL("CRI ADX"),
105
+    .priv_data_size = sizeof(ADXDemuxerContext),
106
+    .read_header    = adx_read_header,
107
+    .read_packet    = adx_read_packet,
108
+    .extensions     = "adx",
109
+    .value          = CODEC_ID_ADPCM_ADX,
110
+};
... ...
@@ -54,6 +54,7 @@ void av_register_all(void)
54 54
     REGISTER_DEMUXER  (ACT, act);
55 55
     REGISTER_DEMUXER  (ADF, adf);
56 56
     REGISTER_MUXER    (ADTS, adts);
57
+    REGISTER_DEMUXER  (ADX, adx);
57 58
     REGISTER_DEMUXER  (AEA, aea);
58 59
     REGISTER_MUXDEMUX (AIFF, aiff);
59 60
     REGISTER_MUXDEMUX (AMR, amr);
... ...
@@ -172,6 +172,7 @@ static int film_read_header(AVFormatContext *s,
172 172
         if (film->audio_type == CODEC_ID_ADPCM_ADX) {
173 173
             st->codec->bits_per_coded_sample = 18 * 8 / 32;
174 174
             st->codec->block_align = st->codec->channels * 18;
175
+            st->need_parsing = AVSTREAM_PARSE_FULL;
175 176
         } else {
176 177
             st->codec->bits_per_coded_sample = film->audio_bits;
177 178
             st->codec->block_align = st->codec->channels *
... ...
@@ -23,7 +23,7 @@
23 23
  * Based on documents from Game Audio Player and own research
24 24
  */
25 25
 
26
-#include "libavutil/bswap.h"
26
+#include "libavutil/intreadwrite.h"
27 27
 #include "avformat.h"
28 28
 #include "pcm.h"
29 29
 
... ...
@@ -33,8 +33,7 @@
33 33
 static int sol_probe(AVProbeData *p)
34 34
 {
35 35
     /* check file header */
36
-    uint16_t magic;
37
-    magic=av_le2ne16(*((uint16_t*)p->buf));
36
+    uint16_t magic = AV_RL32(p->buf);
38 37
     if ((magic == 0x0B8D || magic == 0x0C0D || magic == 0x0C8D) &&
39 38
         p->buf[2] == 'S' && p->buf[3] == 'O' &&
40 39
         p->buf[4] == 'L' && p->buf[5] == 0)
... ...
@@ -24,7 +24,7 @@
24 24
 #include "libavutil/avutil.h"
25 25
 
26 26
 #define LIBAVFORMAT_VERSION_MAJOR 53
27
-#define LIBAVFORMAT_VERSION_MINOR 21
27
+#define LIBAVFORMAT_VERSION_MINOR 22
28 28
 #define LIBAVFORMAT_VERSION_MICRO  0
29 29
 
30 30
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
... ...
@@ -607,7 +607,7 @@ yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
607 607
     const uint8_t * const d128=dither_8x8_220[y&7];
608 608
     uint8_t *g = c->table_gU[128] + c->table_gV[128];
609 609
     int i;
610
-    int acc = 0;
610
+    unsigned acc = 0;
611 611
 
612 612
     for (i = 0; i < dstW - 1; i += 2) {
613 613
         int j;