Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master:
Remove ffmpeg.
aacenc: Simplify windowing
aacenc: Move saved overlap samples to the beginning of the same buffer as incoming samples.
aacenc: Deinterleave input samples before processing.
aacenc: Store channel count in AACEncContext.
aacenc: Move Q^3/4 calculation to it's own table
aacenc: Request normalized float samples instead of converting s16 samples to float.
aacpsy: Replace an if with FFMAX in LAME windowing.
aacenc: cosmetics, replace 'rd' with 'bits' in codebook_trellis_rate to make it more clear what is being calculated.
aacpsy: cosmetics, change a FIXME to a NOTE about subshort comparisons
aacenc: cosmetics: move init() and end() to the bottom of the file.
aacenc: aac_encode_init() cleanup
XWD encoder and decoder
vc1: don't read the interpfrm and bfraction elements for interlaced frames
mxfdec: fix memleak on mxf_read_close()
westwood: split the AUD and VQA demuxers into separate files.

Conflicts:
.gitignore
Changelog
Makefile
configure
doc/ffmpeg.texi
ffmpeg.c
libavcodec/Makefile
libavcodec/aacenc.c
libavcodec/allcodecs.c
libavcodec/avcodec.h
libavcodec/version.h
libavformat/Makefile
libavformat/img2.c

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

Michael Niedermayer authored on 2012/01/24 10:41:53
Showing 21 changed files
... ...
@@ -15,6 +15,7 @@ version next:
15 15
 - amerge audio filter
16 16
 - GSM audio parser
17 17
 - SMJPEG muxer
18
+- XWD encoder and decoder
18 19
 - Automatic thread count based on detection number of (available) CPU cores
19 20
 - y41p Brooktree Uncompressed 4:1:1 12-bit encoder and decoder
20 21
 - ffprobe -show_error option
... ...
@@ -399,6 +399,8 @@ following image formats are supported:
399 399
     @tab YUV, JPEG and some extension is not supported yet.
400 400
 @item Truevision Targa  @tab X @tab X
401 401
     @tab Targa (.TGA) image format
402
+@item XWD  @tab X @tab X
403
+    @tab X Window Dump image format
402 404
 @end multitable
403 405
 
404 406
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -470,6 +470,8 @@ OBJS-$(CONFIG_XBIN_DECODER)            += bintext.o cga_data.o
470 470
 OBJS-$(CONFIG_XL_DECODER)              += xl.o
471 471
 OBJS-$(CONFIG_XSUB_DECODER)            += xsubdec.o
472 472
 OBJS-$(CONFIG_XSUB_ENCODER)            += xsubenc.o
473
+OBJS-$(CONFIG_XWD_DECODER)             += xwddec.o
474
+OBJS-$(CONFIG_XWD_ENCODER)             += xwdenc.o
473 475
 OBJS-$(CONFIG_Y41P_DECODER)            += y41pdec.o
474 476
 OBJS-$(CONFIG_Y41P_ENCODER)            += y41penc.o
475 477
 OBJS-$(CONFIG_YOP_DECODER)             += yop.o
... ...
@@ -110,14 +110,15 @@ static av_always_inline float quantize_and_encode_band_cost_template(
110 110
                                 int *bits, int BT_ZERO, int BT_UNSIGNED,
111 111
                                 int BT_PAIR, int BT_ESC)
112 112
 {
113
-    const float IQ = ff_aac_pow2sf_tab[POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
114
-    const float  Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
113
+    const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
114
+    const float Q   = ff_aac_pow2sf_tab [q_idx];
115
+    const float Q34 = ff_aac_pow34sf_tab[q_idx];
116
+    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
115 117
     const float CLIPPED_ESCAPE = 165140.0f*IQ;
116 118
     int i, j;
117 119
     float cost = 0;
118 120
     const int dim = BT_PAIR ? 2 : 4;
119 121
     int resbits = 0;
120
-    const float  Q34 = sqrtf(Q * sqrtf(Q));
121 122
     const int range  = aac_cb_range[cb];
122 123
     const int maxval = aac_cb_maxval[cb];
123 124
     int off;
... ...
@@ -420,7 +421,7 @@ static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
420 420
     const int run_esc  = (1 << run_bits) - 1;
421 421
     int idx, ppos, count;
422 422
     int stackrun[120], stackcb[120], stack_len;
423
-    float next_minrd = INFINITY;
423
+    float next_minbits = INFINITY;
424 424
     int next_mincb = 0;
425 425
 
426 426
     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
... ...
@@ -434,7 +435,7 @@ static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
434 434
         size = sce->ics.swb_sizes[swb];
435 435
         if (sce->zeroes[win*16 + swb]) {
436 436
             float cost_stay_here = path[swb][0].cost;
437
-            float cost_get_here  = next_minrd + run_bits + 4;
437
+            float cost_get_here  = next_minbits + run_bits + 4;
438 438
             if (   run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
439 439
                 != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
440 440
                 cost_stay_here += run_bits;
... ...
@@ -447,7 +448,7 @@ static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
447 447
                 path[swb+1][0].cost     = cost_stay_here;
448 448
                 path[swb+1][0].run      = path[swb][0].run + 1;
449 449
             }
450
-            next_minrd = path[swb+1][0].cost;
450
+            next_minbits = path[swb+1][0].cost;
451 451
             next_mincb = 0;
452 452
             for (cb = 1; cb < 12; cb++) {
453 453
                 path[swb+1][cb].cost = 61450;
... ...
@@ -455,10 +456,10 @@ static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
455 455
                 path[swb+1][cb].run = 0;
456 456
             }
457 457
         } else {
458
-            float minrd = next_minrd;
458
+            float minbits = next_minbits;
459 459
             int mincb = next_mincb;
460 460
             int startcb = sce->band_type[win*16+swb];
461
-            next_minrd = INFINITY;
461
+            next_minbits = INFINITY;
462 462
             next_mincb = 0;
463 463
             for (cb = 0; cb < startcb; cb++) {
464 464
                 path[swb+1][cb].cost = 61450;
... ...
@@ -467,15 +468,15 @@ static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
467 467
             }
468 468
             for (cb = startcb; cb < 12; cb++) {
469 469
                 float cost_stay_here, cost_get_here;
470
-                float rd = 0.0f;
470
+                float bits = 0.0f;
471 471
                 for (w = 0; w < group_len; w++) {
472
-                    rd += quantize_band_cost(s, sce->coeffs + start + w*128,
473
-                                             s->scoefs + start + w*128, size,
474
-                                             sce->sf_idx[(win+w)*16+swb], cb,
475
-                                             0, INFINITY, NULL);
472
+                    bits += quantize_band_cost(s, sce->coeffs + start + w*128,
473
+                                               s->scoefs + start + w*128, size,
474
+                                               sce->sf_idx[(win+w)*16+swb], cb,
475
+                                               0, INFINITY, NULL);
476 476
                 }
477
-                cost_stay_here = path[swb][cb].cost + rd;
478
-                cost_get_here  = minrd              + rd + run_bits + 4;
477
+                cost_stay_here = path[swb][cb].cost + bits;
478
+                cost_get_here  = minbits            + bits + run_bits + 4;
479 479
                 if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
480 480
                     != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
481 481
                     cost_stay_here += run_bits;
... ...
@@ -488,8 +489,8 @@ static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
488 488
                     path[swb+1][cb].cost     = cost_stay_here;
489 489
                     path[swb+1][cb].run      = path[swb][cb].run + 1;
490 490
                 }
491
-                if (path[swb+1][cb].cost < next_minrd) {
492
-                    next_minrd = path[swb+1][cb].cost;
491
+                if (path[swb+1][cb].cost < next_minbits) {
492
+                    next_minbits = path[swb+1][cb].cost;
493 493
                     next_mincb = cb;
494 494
                 }
495 495
             }
... ...
@@ -46,6 +46,14 @@
46 46
 
47 47
 #define AAC_MAX_CHANNELS 6
48 48
 
49
+#define ERROR_IF(cond, ...) \
50
+    if (cond) { \
51
+        av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
52
+        return AVERROR(EINVAL); \
53
+    }
54
+
55
+float ff_aac_pow34sf_tab[428];
56
+
49 57
 static const uint8_t swb_size_1024_96[] = {
50 58
     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
51 59
     12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
... ...
@@ -136,6 +144,18 @@ static const uint8_t aac_chan_configs[6][5] = {
136 136
 };
137 137
 
138 138
 /**
139
+ * Table to remap channels from Libav's default order to AAC order.
140
+ */
141
+static const uint8_t aac_chan_maps[AAC_MAX_CHANNELS][AAC_MAX_CHANNELS] = {
142
+    { 0 },
143
+    { 0, 1 },
144
+    { 2, 0, 1 },
145
+    { 2, 0, 1, 3 },
146
+    { 2, 0, 1, 3, 4 },
147
+    { 2, 0, 1, 4, 5, 3 },
148
+};
149
+
150
+/**
139 151
  * Make AAC audio config object.
140 152
  * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
141 153
  */
... ...
@@ -147,7 +167,7 @@ static void put_audio_specific_config(AVCodecContext *avctx)
147 147
     init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
148 148
     put_bits(&pb, 5, 2); //object type - AAC-LC
149 149
     put_bits(&pb, 4, s->samplerate_index); //sample rate index
150
-    put_bits(&pb, 4, avctx->channels);
150
+    put_bits(&pb, 4, s->channels);
151 151
     //GASpecificConfig
152 152
     put_bits(&pb, 1, 0); //frame length - 1024 samples
153 153
     put_bits(&pb, 1, 0); //does not depend on core coder
... ...
@@ -160,117 +180,80 @@ static void put_audio_specific_config(AVCodecContext *avctx)
160 160
     flush_put_bits(&pb);
161 161
 }
162 162
 
163
-static av_cold int aac_encode_init(AVCodecContext *avctx)
164
-{
165
-    AACEncContext *s = avctx->priv_data;
166
-    int i;
167
-    const uint8_t *sizes[2];
168
-    uint8_t grouping[AAC_MAX_CHANNELS];
169
-    int lengths[2];
170
-
171
-    avctx->frame_size = 1024;
163
+#define WINDOW_FUNC(type) \
164
+static void apply_ ##type ##_window(DSPContext *dsp, SingleChannelElement *sce, const float *audio)
172 165
 
173
-    for (i = 0; i < 16; i++)
174
-        if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
175
-            break;
176
-    if (i == 16) {
177
-        av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
178
-        return -1;
179
-    }
180
-    if (avctx->channels > AAC_MAX_CHANNELS) {
181
-        av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
182
-        return -1;
183
-    }
184
-    if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
185
-        av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
186
-        return -1;
187
-    }
188
-    if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) {
189
-        av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
190
-        return -1;
191
-    }
192
-    s->samplerate_index = i;
193
-
194
-    dsputil_init(&s->dsp, avctx);
195
-    ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
196
-    ff_mdct_init(&s->mdct128,   8, 0, 1.0);
197
-    // window init
198
-    ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
199
-    ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
200
-    ff_init_ff_sine_windows(10);
201
-    ff_init_ff_sine_windows(7);
202
-
203
-    s->chan_map           = aac_chan_configs[avctx->channels-1];
204
-    s->samples            = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
205
-    s->cpe                = av_mallocz(sizeof(ChannelElement) * s->chan_map[0]);
206
-    avctx->extradata      = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
207
-    avctx->extradata_size = 5;
208
-    put_audio_specific_config(avctx);
166
+WINDOW_FUNC(only_long)
167
+{
168
+    const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
169
+    const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
170
+    float *out = sce->ret;
209 171
 
210
-    sizes[0]   = swb_size_1024[i];
211
-    sizes[1]   = swb_size_128[i];
212
-    lengths[0] = ff_aac_num_swb_1024[i];
213
-    lengths[1] = ff_aac_num_swb_128[i];
214
-    for (i = 0; i < s->chan_map[0]; i++)
215
-        grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
216
-    ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], grouping);
217
-    s->psypp = ff_psy_preprocess_init(avctx);
218
-    s->coder = &ff_aac_coders[s->options.aac_coder];
172
+    dsp->vector_fmul        (out,        audio,        lwindow, 1024);
173
+    dsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
174
+}
219 175
 
220
-    s->lambda = avctx->global_quality ? avctx->global_quality : 120;
176
+WINDOW_FUNC(long_start)
177
+{
178
+    const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
179
+    const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
180
+    float *out = sce->ret;
181
+
182
+    dsp->vector_fmul(out, audio, lwindow, 1024);
183
+    memcpy(out + 1024, audio, sizeof(out[0]) * 448);
184
+    dsp->vector_fmul_reverse(out + 1024 + 448, audio, swindow, 128);
185
+    memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
186
+}
221 187
 
222
-    ff_aac_tableinit();
188
+WINDOW_FUNC(long_stop)
189
+{
190
+    const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
191
+    const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
192
+    float *out = sce->ret;
193
+
194
+    memset(out, 0, sizeof(out[0]) * 448);
195
+    dsp->vector_fmul(out + 448, audio + 448, swindow, 128);
196
+    memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
197
+    dsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
198
+}
223 199
 
224
-    return 0;
200
+WINDOW_FUNC(eight_short)
201
+{
202
+    const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
203
+    const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
204
+    const float *in = audio + 448;
205
+    float *out = sce->ret;
206
+
207
+    for (int w = 0; w < 8; w++) {
208
+        dsp->vector_fmul        (out, in, w ? pwindow : swindow, 128);
209
+        out += 128;
210
+        in  += 128;
211
+        dsp->vector_fmul_reverse(out, in, swindow, 128);
212
+        out += 128;
213
+    }
225 214
 }
226 215
 
227
-static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
228
-                                  SingleChannelElement *sce, short *audio)
216
+static void (*const apply_window[4])(DSPContext *dsp, SingleChannelElement *sce, const float *audio) = {
217
+    [ONLY_LONG_SEQUENCE]   = apply_only_long_window,
218
+    [LONG_START_SEQUENCE]  = apply_long_start_window,
219
+    [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
220
+    [LONG_STOP_SEQUENCE]   = apply_long_stop_window
221
+};
222
+
223
+static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce,
224
+                                  float *audio)
229 225
 {
230
-    int i, k;
231
-    const int chans = avctx->channels;
232
-    const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
233
-    const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
234
-    const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
226
+    int i;
235 227
     float *output = sce->ret;
236 228
 
237
-    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
238
-        memcpy(output, sce->saved, sizeof(float)*1024);
239
-        if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
240
-            memset(output, 0, sizeof(output[0]) * 448);
241
-            for (i = 448; i < 576; i++)
242
-                output[i] = sce->saved[i] * pwindow[i - 448];
243
-            for (i = 576; i < 704; i++)
244
-                output[i] = sce->saved[i];
245
-        }
246
-        if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
247
-            for (i = 0; i < 1024; i++) {
248
-                output[i+1024]         = audio[i * chans] * lwindow[1024 - i - 1];
249
-                sce->saved[i] = audio[i * chans] * lwindow[i];
250
-            }
251
-        } else {
252
-            for (i = 0; i < 448; i++)
253
-                output[i+1024]         = audio[i * chans];
254
-            for (; i < 576; i++)
255
-                output[i+1024]         = audio[i * chans] * swindow[576 - i - 1];
256
-            memset(output+1024+576, 0, sizeof(output[0]) * 448);
257
-            for (i = 0; i < 1024; i++)
258
-                sce->saved[i] = audio[i * chans];
259
-        }
229
+    apply_window[sce->ics.window_sequence[0]](&s->dsp, sce, audio);
230
+
231
+    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE)
260 232
         s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
261
-    } else {
262
-        for (k = 0; k < 1024; k += 128) {
263
-            for (i = 448 + k; i < 448 + k + 256; i++)
264
-                output[i - 448 - k] = (i < 1024)
265
-                                         ? sce->saved[i]
266
-                                         : audio[(i-1024)*chans];
267
-            s->dsp.vector_fmul        (output,     output, k ?  swindow : pwindow, 128);
268
-            s->dsp.vector_fmul_reverse(output+128, output+128, swindow, 128);
269
-            s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + k, output);
270
-        }
271
-        for (i = 0; i < 1024; i++)
272
-            sce->saved[i] = audio[i * chans];
273
-    }
233
+    else
234
+        for (i = 0; i < 1024; i += 128)
235
+            s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + i, output + i*2);
236
+    memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
274 237
 }
275 238
 
276 239
 /**
... ...
@@ -488,11 +471,37 @@ static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
488 488
     put_bits(&s->pb, 12 - padbits, 0);
489 489
 }
490 490
 
491
+/*
492
+ * Deinterleave input samples.
493
+ * Channels are reordered from Libav's default order to AAC order.
494
+ */
495
+static void deinterleave_input_samples(AACEncContext *s,
496
+                                       const float *samples)
497
+{
498
+    int ch, i;
499
+    const int sinc = s->channels;
500
+    const uint8_t *channel_map = aac_chan_maps[sinc - 1];
501
+
502
+    /* deinterleave and remap input samples */
503
+    for (ch = 0; ch < sinc; ch++) {
504
+        const float *sptr = samples + channel_map[ch];
505
+
506
+        /* copy last 1024 samples of previous frame to the start of the current frame */
507
+        memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][1024], 1024 * sizeof(s->planar_samples[0][0]));
508
+
509
+        /* deinterleave */
510
+        for (i = 1024; i < 1024 * 2; i++) {
511
+            s->planar_samples[ch][i] = *sptr;
512
+            sptr += sinc;
513
+        }
514
+    }
515
+}
516
+
491 517
 static int aac_encode_frame(AVCodecContext *avctx,
492 518
                             uint8_t *frame, int buf_size, void *data)
493 519
 {
494 520
     AACEncContext *s = avctx->priv_data;
495
-    int16_t *samples = s->samples, *samples2, *la;
521
+    float **samples = s->planar_samples, *samples2, *la, *overlap;
496 522
     ChannelElement *cpe;
497 523
     int i, ch, w, g, chans, tag, start_ch;
498 524
     int chan_el_counter[4];
... ...
@@ -500,27 +509,15 @@ static int aac_encode_frame(AVCodecContext *avctx,
500 500
 
501 501
     if (s->last_frame)
502 502
         return 0;
503
+
503 504
     if (data) {
504
-        if (!s->psypp) {
505
-            memcpy(s->samples + 1024 * avctx->channels, data,
506
-                   1024 * avctx->channels * sizeof(s->samples[0]));
507
-        } else {
508
-            start_ch = 0;
509
-            samples2 = s->samples + 1024 * avctx->channels;
510
-            for (i = 0; i < s->chan_map[0]; i++) {
511
-                tag = s->chan_map[i+1];
512
-                chans = tag == TYPE_CPE ? 2 : 1;
513
-                ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch,
514
-                                  samples2 + start_ch, start_ch, chans);
515
-                start_ch += chans;
516
-            }
517
-        }
505
+        deinterleave_input_samples(s, data);
506
+        if (s->psypp)
507
+            ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
518 508
     }
519
-    if (!avctx->frame_number) {
520
-        memcpy(s->samples, s->samples + 1024 * avctx->channels,
521
-               1024 * avctx->channels * sizeof(s->samples[0]));
509
+
510
+    if (!avctx->frame_number)
522 511
         return 0;
523
-    }
524 512
 
525 513
     start_ch = 0;
526 514
     for (i = 0; i < s->chan_map[0]; i++) {
... ...
@@ -531,8 +528,9 @@ static int aac_encode_frame(AVCodecContext *avctx,
531 531
         for (ch = 0; ch < chans; ch++) {
532 532
             IndividualChannelStream *ics = &cpe->ch[ch].ics;
533 533
             int cur_channel = start_ch + ch;
534
-            samples2 = samples + cur_channel;
535
-            la       = samples2 + (448+64) * avctx->channels;
534
+            overlap  = &samples[cur_channel][0];
535
+            samples2 = overlap + 1024;
536
+            la       = samples2 + (448+64);
536 537
             if (!data)
537 538
                 la = NULL;
538 539
             if (tag == TYPE_LFE) {
... ...
@@ -560,7 +558,7 @@ static int aac_encode_frame(AVCodecContext *avctx,
560 560
             for (w = 0; w < ics->num_windows; w++)
561 561
                 ics->group_len[w] = wi[ch].grouping[w];
562 562
 
563
-            apply_window_and_mdct(avctx, s, &cpe->ch[ch], samples2);
563
+            apply_window_and_mdct(s, &cpe->ch[ch], overlap);
564 564
         }
565 565
         start_ch += chans;
566 566
     }
... ...
@@ -626,8 +624,8 @@ static int aac_encode_frame(AVCodecContext *avctx,
626 626
         }
627 627
 
628 628
         frame_bits = put_bits_count(&s->pb);
629
-        if (frame_bits <= 6144 * avctx->channels - 3) {
630
-            s->psy.bitres.bits = frame_bits / avctx->channels;
629
+        if (frame_bits <= 6144 * s->channels - 3) {
630
+            s->psy.bitres.bits = frame_bits / s->channels;
631 631
             break;
632 632
         }
633 633
 
... ...
@@ -648,8 +646,7 @@ static int aac_encode_frame(AVCodecContext *avctx,
648 648
 
649 649
     if (!data)
650 650
         s->last_frame = 1;
651
-    memcpy(s->samples, s->samples + 1024 * avctx->channels,
652
-           1024 * avctx->channels * sizeof(s->samples[0]));
651
+
653 652
     return put_bits_count(&s->pb)>>3;
654 653
 }
655 654
 
... ...
@@ -660,12 +657,109 @@ static av_cold int aac_encode_end(AVCodecContext *avctx)
660 660
     ff_mdct_end(&s->mdct1024);
661 661
     ff_mdct_end(&s->mdct128);
662 662
     ff_psy_end(&s->psy);
663
-    ff_psy_preprocess_end(s->psypp);
664
-    av_freep(&s->samples);
663
+    if (s->psypp)
664
+        ff_psy_preprocess_end(s->psypp);
665
+    av_freep(&s->buffer.samples);
665 666
     av_freep(&s->cpe);
666 667
     return 0;
667 668
 }
668 669
 
670
+static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
671
+{
672
+    int ret = 0;
673
+
674
+    dsputil_init(&s->dsp, avctx);
675
+
676
+    // window init
677
+    ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
678
+    ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
679
+    ff_init_ff_sine_windows(10);
680
+    ff_init_ff_sine_windows(7);
681
+
682
+    if (ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0))
683
+        return ret;
684
+    if (ret = ff_mdct_init(&s->mdct128,   8, 0, 32768.0))
685
+        return ret;
686
+
687
+    return 0;
688
+}
689
+
690
+static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
691
+{
692
+    FF_ALLOCZ_OR_GOTO(avctx, s->buffer.samples, 3 * 1024 * s->channels * sizeof(s->buffer.samples[0]), alloc_fail);
693
+    FF_ALLOCZ_OR_GOTO(avctx, s->cpe, sizeof(ChannelElement) * s->chan_map[0], alloc_fail);
694
+    FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + FF_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
695
+
696
+    for(int ch = 0; ch < s->channels; ch++)
697
+        s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
698
+
699
+    return 0;
700
+alloc_fail:
701
+    return AVERROR(ENOMEM);
702
+}
703
+
704
+static av_cold int aac_encode_init(AVCodecContext *avctx)
705
+{
706
+    AACEncContext *s = avctx->priv_data;
707
+    int i, ret = 0;
708
+    const uint8_t *sizes[2];
709
+    uint8_t grouping[AAC_MAX_CHANNELS];
710
+    int lengths[2];
711
+
712
+    avctx->frame_size = 1024;
713
+
714
+    for (i = 0; i < 16; i++)
715
+        if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
716
+            break;
717
+
718
+    s->channels = avctx->channels;
719
+
720
+    ERROR_IF(i == 16,
721
+             "Unsupported sample rate %d\n", avctx->sample_rate);
722
+    ERROR_IF(s->channels > AAC_MAX_CHANNELS,
723
+             "Unsupported number of channels: %d\n", s->channels);
724
+    ERROR_IF(avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW,
725
+             "Unsupported profile %d\n", avctx->profile);
726
+    ERROR_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
727
+             "Too many bits per frame requested\n");
728
+
729
+    s->samplerate_index = i;
730
+
731
+    s->chan_map = aac_chan_configs[s->channels-1];
732
+
733
+    if (ret = dsp_init(avctx, s))
734
+        goto fail;
735
+
736
+    if (ret = alloc_buffers(avctx, s))
737
+        goto fail;
738
+
739
+    avctx->extradata_size = 5;
740
+    put_audio_specific_config(avctx);
741
+
742
+    sizes[0]   = swb_size_1024[i];
743
+    sizes[1]   = swb_size_128[i];
744
+    lengths[0] = ff_aac_num_swb_1024[i];
745
+    lengths[1] = ff_aac_num_swb_128[i];
746
+    for (i = 0; i < s->chan_map[0]; i++)
747
+        grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
748
+    if (ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], grouping))
749
+        goto fail;
750
+    s->psypp = ff_psy_preprocess_init(avctx);
751
+    s->coder = &ff_aac_coders[s->options.aac_coder];
752
+
753
+    s->lambda = avctx->global_quality ? avctx->global_quality : 120;
754
+
755
+    ff_aac_tableinit();
756
+
757
+    for (i = 0; i < 428; i++)
758
+        ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
759
+
760
+    return 0;
761
+fail:
762
+    aac_encode_end(avctx);
763
+    return ret;
764
+}
765
+
669 766
 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
670 767
 static const AVOption aacenc_options[] = {
671 768
     {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), AV_OPT_TYPE_INT, {.dbl = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
... ...
@@ -692,7 +786,7 @@ AVCodec ff_aac_encoder = {
692 692
     .encode         = aac_encode_frame,
693 693
     .close          = aac_encode_end,
694 694
     .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
695
-    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
695
+    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
696 696
     .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
697 697
     .priv_class = &aacenc_class,
698 698
 };
... ...
@@ -61,9 +61,10 @@ typedef struct AACEncContext {
61 61
     FFTContext mdct1024;                         ///< long (1024 samples) frame transform context
62 62
     FFTContext mdct128;                          ///< short (128 samples) frame transform context
63 63
     DSPContext  dsp;
64
-    int16_t *samples;                            ///< saved preprocessed input
64
+    float *planar_samples[6];                    ///< saved preprocessed input
65 65
 
66 66
     int samplerate_index;                        ///< MPEG-4 samplerate index
67
+    int channels;                                ///< channel count
67 68
     const uint8_t *chan_map;                     ///< channel configuration map
68 69
 
69 70
     ChannelElement *cpe;                         ///< channel elements
... ...
@@ -75,6 +76,12 @@ typedef struct AACEncContext {
75 75
     float lambda;
76 76
     DECLARE_ALIGNED(16, int,   qcoefs)[96];      ///< quantized coefficients
77 77
     DECLARE_ALIGNED(32, float, scoefs)[1024];    ///< scaled coefficients
78
+
79
+    struct {
80
+        float *samples;
81
+    } buffer;
78 82
 } AACEncContext;
79 83
 
84
+extern float ff_aac_pow34sf_tab[428];
85
+
80 86
 #endif /* AVCODEC_AACENC_H */
... ...
@@ -400,7 +400,7 @@ static av_unused FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
400 400
         int stay_short = 0;
401 401
         for (i = 0; i < 8; i++) {
402 402
             for (j = 0; j < 128; j++) {
403
-                v = iir_filter(la[(i*128+j)*ctx->avctx->channels], pch->iir_state);
403
+                v = iir_filter(la[i*128+j], pch->iir_state);
404 404
                 sum += v*v;
405 405
             }
406 406
             s[i]  = sum;
... ...
@@ -776,9 +776,8 @@ static void lame_apply_block_type(AacPsyChannel *ctx, FFPsyWindowInfo *wi, int u
776 776
     ctx->next_window_seq = blocktype;
777 777
 }
778 778
 
779
-static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx,
780
-                                       const int16_t *audio, const int16_t *la,
781
-                                       int channel, int prev_type)
779
+static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx, const float *audio,
780
+                                       const float *la, int channel, int prev_type)
782 781
 {
783 782
     AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
784 783
     AacPsyChannel *pch  = &pctx->ch[channel];
... ...
@@ -795,20 +794,20 @@ static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx,
795 795
         float attack_intensity[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
796 796
         float energy_subshort[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
797 797
         float energy_short[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
798
-        int chans = ctx->avctx->channels;
799
-        const int16_t *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN) * chans;
798
+        const float *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN);
800 799
         int j, att_sum = 0;
801 800
 
802 801
         /* LAME comment: apply high pass filter of fs/4 */
803 802
         for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
804 803
             float sum1, sum2;
805
-            sum1 = firbuf[(i + ((PSY_LAME_FIR_LEN - 1) / 2)) * chans];
804
+            sum1 = firbuf[i + (PSY_LAME_FIR_LEN - 1) / 2];
806 805
             sum2 = 0.0;
807 806
             for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
808
-                sum1 += psy_fir_coeffs[j] * (firbuf[(i + j) * chans] + firbuf[(i + PSY_LAME_FIR_LEN - j) * chans]);
809
-                sum2 += psy_fir_coeffs[j + 1] * (firbuf[(i + j + 1) * chans] + firbuf[(i + PSY_LAME_FIR_LEN - j - 1) * chans]);
807
+                sum1 += psy_fir_coeffs[j] * (firbuf[i + j] + firbuf[i + PSY_LAME_FIR_LEN - j]);
808
+                sum2 += psy_fir_coeffs[j + 1] * (firbuf[i + j + 1] + firbuf[i + PSY_LAME_FIR_LEN - j - 1]);
810 809
             }
811
-            hpfsmpl[i] = sum1 + sum2;
810
+            /* NOTE: The LAME psymodel expects it's input in the range -32768 to 32768. Tuning this for normalized floats would be difficult. */
811
+            hpfsmpl[i] = (sum1 + sum2) * 32768.0f;
812 812
         }
813 813
 
814 814
         /* Calculate the energies of each sub-shortblock */
... ...
@@ -823,16 +822,15 @@ static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx,
823 823
             float const *const pfe = pf + AAC_BLOCK_SIZE_LONG / (AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS);
824 824
             float p = 1.0f;
825 825
             for (; pf < pfe; pf++)
826
-                if (p < fabsf(*pf))
827
-                    p = fabsf(*pf);
826
+                p = FFMAX(p, fabsf(*pf));
828 827
             pch->prev_energy_subshort[i] = energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS] = p;
829 828
             energy_short[1 + i / PSY_LAME_NUM_SUBBLOCKS] += p;
830
-            /* FIXME: The indexes below are [i + 3 - 2] in the LAME source.
831
-             *          Obviously the 3 and 2 have some significance, or this would be just [i + 1]
832
-             *          (which is what we use here). What the 3 stands for is ambigious, as it is both
833
-             *          number of short blocks, and the number of sub-short blocks.
834
-             *          It seems that LAME is comparing each sub-block to sub-block + 1 in the
835
-             *          previous block.
829
+            /* NOTE: The indexes below are [i + 3 - 2] in the LAME source.
830
+             *       Obviously the 3 and 2 have some significance, or this would be just [i + 1]
831
+             *       (which is what we use here). What the 3 stands for is ambiguous, as it is both
832
+             *       number of short blocks, and the number of sub-short blocks.
833
+             *       It seems that LAME is comparing each sub-block to sub-block + 1 in the
834
+             *       previous block.
836 835
              */
837 836
             if (p > energy_subshort[i + 1])
838 837
                 p = p / energy_subshort[i + 1];
... ...
@@ -245,6 +245,7 @@ void avcodec_register_all(void)
245 245
     REGISTER_DECODER (XAN_WC3, xan_wc3);
246 246
     REGISTER_DECODER (XAN_WC4, xan_wc4);
247 247
     REGISTER_DECODER (XL, xl);
248
+    REGISTER_ENCDEC  (XWD, xwd);
248 249
     REGISTER_ENCDEC  (Y41P, y41p);
249 250
     REGISTER_DECODER (YOP, yop);
250 251
     REGISTER_ENCDEC  (YUV4, yuv4);
... ...
@@ -255,6 +255,7 @@ enum CodecID {
255 255
     CODEC_ID_VBLE,
256 256
     CODEC_ID_DXTORY,
257 257
     CODEC_ID_V410,
258
+    CODEC_ID_XWD,
258 259
     CODEC_ID_Y41P       = MKBETAG('Y','4','1','P'),
259 260
     CODEC_ID_UTVIDEO = 0x800,
260 261
     CODEC_ID_ESCAPE130  = MKBETAG('E','1','3','0'),
... ...
@@ -112,20 +112,15 @@ av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *av
112 112
     return ctx;
113 113
 }
114 114
 
115
-void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
116
-                       const int16_t *audio, int16_t *dest,
117
-                       int tag, int channels)
115
+void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels)
118 116
 {
119
-    int ch, i;
117
+    int ch;
118
+    int frame_size = ctx->avctx->frame_size;
119
+
120 120
     if (ctx->fstate) {
121 121
         for (ch = 0; ch < channels; ch++)
122
-            ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size,
123
-                          audio + ch, ctx->avctx->channels,
124
-                          dest  + ch, ctx->avctx->channels);
125
-    } else {
126
-        for (ch = 0; ch < channels; ch++)
127
-            for (i = 0; i < ctx->avctx->frame_size; i++)
128
-                dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch];
122
+            ff_iir_filter_flt(ctx->fcoeffs, ctx->fstate[ch], frame_size,
123
+                              &audio[ch][frame_size], 1, &audio[ch][frame_size], 1);
129 124
     }
130 125
 }
131 126
 
... ...
@@ -109,7 +109,7 @@ typedef struct FFPsyModel {
109 109
      *
110 110
      * @return suggested window information in a structure
111 111
      */
112
-    FFPsyWindowInfo (*window)(FFPsyContext *ctx, const int16_t *audio, const int16_t *la, int channel, int prev_type);
112
+    FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
113 113
 
114 114
     /**
115 115
      * Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
... ...
@@ -174,14 +174,10 @@ av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *av
174 174
  * Preprocess several channel in audio frame in order to compress it better.
175 175
  *
176 176
  * @param ctx      preprocessing context
177
- * @param audio    samples to preprocess
178
- * @param dest     place to put filtered samples
179
- * @param tag      channel number
180
- * @param channels number of channel to preprocess (some additional work may be done on stereo pair)
177
+ * @param audio    samples to be filtered (in place)
178
+ * @param channels number of channel to preprocess
181 179
  */
182
-void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
183
-                       const int16_t *audio, int16_t *dest,
184
-                       int tag, int channels);
180
+void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
185 181
 
186 182
 /**
187 183
  * Cleanup audio preprocessing module.
... ...
@@ -918,13 +918,15 @@ int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
918 918
         }
919 919
         goto parse_common_info;
920 920
     }
921
-    if (v->finterpflag)
922
-        v->interpfrm = get_bits1(gb);
923
-    if (v->s.pict_type == AV_PICTURE_TYPE_B) {
924
-        v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
925
-        v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
926
-        if (v->bfraction == 0) {
927
-            v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
921
+    if (v->fcm == PROGRESSIVE) {
922
+        if (v->finterpflag)
923
+            v->interpfrm = get_bits1(gb);
924
+        if (v->s.pict_type == AV_PICTURE_TYPE_B) {
925
+            v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
926
+            v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
927
+            if (v->bfraction == 0) {
928
+                v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
929
+            }
928 930
         }
929 931
     }
930 932
 
... ...
@@ -21,8 +21,8 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR 57
25
-#define LIBAVCODEC_VERSION_MICRO 105
24
+#define LIBAVCODEC_VERSION_MINOR 58
25
+#define LIBAVCODEC_VERSION_MICRO 100
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
                                                LIBAVCODEC_VERSION_MINOR, \
29 29
new file mode 100644
... ...
@@ -0,0 +1,41 @@
0
+/*
1
+ * XWD image format
2
+ *
3
+ * Copyright (c) 2012 Paul B Mahol
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * Libav is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AVCODEC_XWD_H
23
+#define AVCODEC_XWD_H
24
+
25
+#define XWD_VERSION         7
26
+#define XWD_HEADER_SIZE     100
27
+#define XWD_CMAP_SIZE       12
28
+
29
+#define XWD_XY_BITMAP       0
30
+#define XWD_XY_PIXMAP       1
31
+#define XWD_Z_PIXMAP        2
32
+
33
+#define XWD_STATIC_GRAY     0
34
+#define XWD_GRAY_SCALE      1
35
+#define XWD_STATIC_COLOR    2
36
+#define XWD_PSEUDO_COLOR    3
37
+#define XWD_TRUE_COLOR      4
38
+#define XWD_DIRECT_COLOR    5
39
+
40
+#endif /* AVCODEC_XWD_H */
0 41
new file mode 100644
... ...
@@ -0,0 +1,267 @@
0
+/*
1
+ * XWD image format
2
+ *
3
+ * Copyright (c) 2012 Paul B Mahol
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * Libav is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "libavutil/imgutils.h"
23
+#include "avcodec.h"
24
+#include "bytestream.h"
25
+#include "xwd.h"
26
+
27
+static av_cold int xwd_decode_init(AVCodecContext *avctx)
28
+{
29
+    avctx->coded_frame = avcodec_alloc_frame();
30
+    if (!avctx->coded_frame)
31
+        return AVERROR(ENOMEM);
32
+
33
+    return 0;
34
+}
35
+
36
+static int xwd_decode_frame(AVCodecContext *avctx, void *data,
37
+                            int *data_size, AVPacket *avpkt)
38
+{
39
+    AVFrame *p = avctx->coded_frame;
40
+    const uint8_t *buf = avpkt->data;
41
+    int i, ret, buf_size = avpkt->size;
42
+    uint32_t version, header_size, vclass, ncolors;
43
+    uint32_t xoffset, be, bpp, lsize, rsize;
44
+    uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
45
+    uint32_t rgb[3];
46
+    uint8_t *ptr;
47
+
48
+    if (buf_size < XWD_HEADER_SIZE)
49
+        return AVERROR_INVALIDDATA;
50
+
51
+    header_size = bytestream_get_be32(&buf);
52
+    if (buf_size < header_size)
53
+        return AVERROR_INVALIDDATA;
54
+
55
+    version = bytestream_get_be32(&buf);
56
+    if (version != XWD_VERSION) {
57
+        av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
58
+        return AVERROR_INVALIDDATA;
59
+    }
60
+
61
+    if (header_size < XWD_HEADER_SIZE) {
62
+        av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
63
+        return AVERROR_INVALIDDATA;
64
+    }
65
+
66
+    pixformat     = bytestream_get_be32(&buf);
67
+    pixdepth      = bytestream_get_be32(&buf);
68
+    avctx->width  = bytestream_get_be32(&buf);
69
+    avctx->height = bytestream_get_be32(&buf);
70
+    xoffset       = bytestream_get_be32(&buf);
71
+    be            = bytestream_get_be32(&buf);
72
+    bunit         = bytestream_get_be32(&buf);
73
+    bitorder      = bytestream_get_be32(&buf);
74
+    bpad          = bytestream_get_be32(&buf);
75
+    bpp           = bytestream_get_be32(&buf);
76
+    lsize         = bytestream_get_be32(&buf);
77
+    vclass        = bytestream_get_be32(&buf);
78
+    rgb[0]        = bytestream_get_be32(&buf);
79
+    rgb[1]        = bytestream_get_be32(&buf);
80
+    rgb[2]        = bytestream_get_be32(&buf);
81
+    buf          += 8;
82
+    ncolors       = bytestream_get_be32(&buf);
83
+    buf          += header_size - (XWD_HEADER_SIZE - 20);
84
+
85
+    av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n",
86
+           pixformat, pixdepth, bunit, bitorder, bpad);
87
+    av_log(avctx, AV_LOG_DEBUG, "vclass %d, ncolors %d, bpp %d, be %d, lsize %d, xoffset %d\n",
88
+           vclass, ncolors, bpp, be, lsize, xoffset);
89
+    av_log(avctx, AV_LOG_DEBUG, "red %0x, green %0x, blue %0x\n", rgb[0], rgb[1], rgb[2]);
90
+
91
+    if (pixformat > XWD_Z_PIXMAP) {
92
+        av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
93
+        return AVERROR_INVALIDDATA;
94
+    }
95
+
96
+    if (pixdepth == 0 || pixdepth > 32) {
97
+        av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
98
+        return AVERROR_INVALIDDATA;
99
+    }
100
+
101
+    if (xoffset) {
102
+        av_log_ask_for_sample(avctx, "unsupported xoffset %d\n", xoffset);
103
+        return AVERROR_PATCHWELCOME;
104
+    }
105
+
106
+    if (be > 1) {
107
+        av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
108
+        return AVERROR_INVALIDDATA;
109
+    }
110
+
111
+    if (bitorder > 1) {
112
+        av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
113
+        return AVERROR_INVALIDDATA;
114
+    }
115
+
116
+    if (bunit != 8 && bunit != 16 && bunit != 32) {
117
+        av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
118
+        return AVERROR_INVALIDDATA;
119
+    }
120
+
121
+    if (bpad != 8 && bpad != 16 && bpad != 32) {
122
+        av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
123
+        return AVERROR_INVALIDDATA;
124
+    }
125
+
126
+    if (bpp == 0 || bpp > 32) {
127
+        av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
128
+        return AVERROR_INVALIDDATA;
129
+    }
130
+
131
+    if (ncolors > 256) {
132
+        av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
133
+        return AVERROR_INVALIDDATA;
134
+    }
135
+
136
+    if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
137
+        return ret;
138
+
139
+    rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
140
+    if (lsize < rsize) {
141
+        av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
142
+        return AVERROR_INVALIDDATA;
143
+    }
144
+
145
+    if (buf_size < header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize) {
146
+        av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
147
+        return AVERROR_INVALIDDATA;
148
+    }
149
+
150
+    if (pixformat != XWD_Z_PIXMAP) {
151
+        av_log(avctx, AV_LOG_ERROR, "pixmap format %d unsupported\n", pixformat);
152
+        return AVERROR_PATCHWELCOME;
153
+    }
154
+
155
+    avctx->pix_fmt = PIX_FMT_NONE;
156
+    switch (vclass) {
157
+    case XWD_STATIC_GRAY:
158
+    case XWD_GRAY_SCALE:
159
+        if (bpp != 1)
160
+            return AVERROR_INVALIDDATA;
161
+        if (pixdepth == 1)
162
+            avctx->pix_fmt = PIX_FMT_MONOWHITE;
163
+        break;
164
+    case XWD_STATIC_COLOR:
165
+    case XWD_PSEUDO_COLOR:
166
+        if (bpp == 8)
167
+            avctx->pix_fmt = PIX_FMT_PAL8;
168
+        break;
169
+    case XWD_TRUE_COLOR:
170
+    case XWD_DIRECT_COLOR:
171
+        if (bpp != 16 && bpp != 24 && bpp != 32)
172
+            return AVERROR_INVALIDDATA;
173
+        if (bpp == 16 && pixdepth == 15) {
174
+            if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
175
+                avctx->pix_fmt = be ? PIX_FMT_RGB555BE : PIX_FMT_RGB555LE;
176
+            else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
177
+                avctx->pix_fmt = be ? PIX_FMT_BGR555BE : PIX_FMT_BGR555LE;
178
+        } else if (bpp == 16 && pixdepth == 16) {
179
+            if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
180
+                avctx->pix_fmt = be ? PIX_FMT_RGB565BE : PIX_FMT_RGB565LE;
181
+            else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
182
+                avctx->pix_fmt = be ? PIX_FMT_BGR565BE : PIX_FMT_BGR565LE;
183
+        } else if (bpp == 24) {
184
+            if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
185
+                avctx->pix_fmt = be ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
186
+            else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
187
+                avctx->pix_fmt = be ? PIX_FMT_BGR24 : PIX_FMT_RGB24;
188
+        } else if (bpp == 32) {
189
+            if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
190
+                avctx->pix_fmt = be ? PIX_FMT_ARGB : PIX_FMT_BGRA;
191
+            else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
192
+                avctx->pix_fmt = be ? PIX_FMT_ABGR : PIX_FMT_RGBA;
193
+        }
194
+        buf += ncolors * XWD_CMAP_SIZE;
195
+        break;
196
+    default:
197
+        av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
198
+        return AVERROR_INVALIDDATA;
199
+    }
200
+
201
+    if (avctx->pix_fmt == PIX_FMT_NONE) {
202
+        av_log_ask_for_sample(avctx, "unknown file: bpp %d, pixdepth %d, vclass %d\n", bpp, pixdepth, vclass);
203
+        return AVERROR_PATCHWELCOME;
204
+    }
205
+
206
+    if (p->data[0])
207
+        avctx->release_buffer(avctx, p);
208
+
209
+    p->reference = 0;
210
+    if ((ret = avctx->get_buffer(avctx, p)) < 0) {
211
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
212
+        return ret;
213
+    }
214
+
215
+    p->key_frame = 1;
216
+    p->pict_type = AV_PICTURE_TYPE_I;
217
+
218
+    if (avctx->pix_fmt == PIX_FMT_PAL8) {
219
+        uint32_t *dst = (uint32_t *)p->data[1];
220
+        uint8_t red, green, blue;
221
+
222
+        for (i = 0; i < ncolors; i++) {
223
+
224
+            buf   += 4;  // skip colormap entry number
225
+            red    = *buf; buf += 2;
226
+            green  = *buf; buf += 2;
227
+            blue   = *buf; buf += 2;
228
+            buf   += 2;  // skip bitmask flag and padding
229
+
230
+            dst[i] = red << 16 | green << 8 | blue;
231
+        }
232
+    }
233
+
234
+    ptr = p->data[0];
235
+    for (i = 0; i < avctx->height; i++) {
236
+        bytestream_get_buffer(&buf, ptr, rsize);
237
+        buf += lsize - rsize;
238
+        ptr += p->linesize[0];
239
+    }
240
+
241
+    *data_size = sizeof(AVFrame);
242
+    *(AVFrame *)data = *p;
243
+
244
+    return buf_size;
245
+}
246
+
247
+static av_cold int xwd_decode_close(AVCodecContext *avctx)
248
+{
249
+    if (avctx->coded_frame->data[0])
250
+        avctx->release_buffer(avctx, avctx->coded_frame);
251
+
252
+    av_freep(&avctx->coded_frame);
253
+
254
+    return 0;
255
+}
256
+
257
+AVCodec ff_xwd_decoder = {
258
+    .name           = "xwd",
259
+    .type           = AVMEDIA_TYPE_VIDEO,
260
+    .id             = CODEC_ID_XWD,
261
+    .init           = xwd_decode_init,
262
+    .close          = xwd_decode_close,
263
+    .decode         = xwd_decode_frame,
264
+    .capabilities   = CODEC_CAP_DR1,
265
+    .long_name      = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
266
+};
0 267
new file mode 100644
... ...
@@ -0,0 +1,246 @@
0
+/*
1
+ * XWD image format
2
+ *
3
+ * Copyright (c) 2012 Paul B Mahol
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * Libav is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "libavutil/intreadwrite.h"
23
+#include "libavutil/pixdesc.h"
24
+#include "avcodec.h"
25
+#include "bytestream.h"
26
+#include "xwd.h"
27
+
28
+#define WINDOW_NAME         "lavcxwdenc"
29
+#define WINDOW_NAME_SIZE    11
30
+
31
+static av_cold int xwd_encode_init(AVCodecContext *avctx)
32
+{
33
+    avctx->coded_frame = avcodec_alloc_frame();
34
+    if (!avctx->coded_frame)
35
+        return AVERROR(ENOMEM);
36
+
37
+    return 0;
38
+}
39
+
40
+static int xwd_encode_frame(AVCodecContext *avctx, uint8_t *buf,
41
+                            int buf_size, void *data)
42
+{
43
+    AVFrame *p = data;
44
+    enum PixelFormat pix_fmt = avctx->pix_fmt;
45
+    uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
46
+    uint32_t rgb[3] = { 0 };
47
+    uint32_t header_size;
48
+    int i, out_size;
49
+    uint8_t *ptr;
50
+
51
+    pixdepth = av_get_bits_per_pixel(&av_pix_fmt_descriptors[pix_fmt]);
52
+    if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BE)
53
+        be = 1;
54
+    switch (pix_fmt) {
55
+    case PIX_FMT_ARGB:
56
+    case PIX_FMT_BGRA:
57
+    case PIX_FMT_RGBA:
58
+    case PIX_FMT_ABGR:
59
+        if (pix_fmt == PIX_FMT_ARGB ||
60
+            pix_fmt == PIX_FMT_ABGR)
61
+            be = 1;
62
+        if (pix_fmt == PIX_FMT_ABGR ||
63
+            pix_fmt == PIX_FMT_RGBA) {
64
+            rgb[0] = 0xFF;
65
+            rgb[1] = 0xFF00;
66
+            rgb[2] = 0xFF0000;
67
+        } else {
68
+            rgb[0] = 0xFF0000;
69
+            rgb[1] = 0xFF00;
70
+            rgb[2] = 0xFF;
71
+        }
72
+        bpp      = 32;
73
+        pixdepth = 24;
74
+        vclass   = XWD_TRUE_COLOR;
75
+        bpad     = 32;
76
+        break;
77
+    case PIX_FMT_BGR24:
78
+    case PIX_FMT_RGB24:
79
+        if (pix_fmt == PIX_FMT_RGB24)
80
+            be = 1;
81
+        bpp      = 24;
82
+        vclass   = XWD_TRUE_COLOR;
83
+        bpad     = 32;
84
+        rgb[0]   = 0xFF0000;
85
+        rgb[1]   = 0xFF00;
86
+        rgb[2]   = 0xFF;
87
+        break;
88
+    case PIX_FMT_RGB565LE:
89
+    case PIX_FMT_RGB565BE:
90
+    case PIX_FMT_BGR565LE:
91
+    case PIX_FMT_BGR565BE:
92
+        if (pix_fmt == PIX_FMT_BGR565LE ||
93
+            pix_fmt == PIX_FMT_BGR565BE) {
94
+            rgb[0] = 0x1F;
95
+            rgb[1] = 0x7E0;
96
+            rgb[2] = 0xF800;
97
+        } else {
98
+            rgb[0] = 0xF800;
99
+            rgb[1] = 0x7E0;
100
+            rgb[2] = 0x1F;
101
+        }
102
+        bpp      = 16;
103
+        vclass   = XWD_TRUE_COLOR;
104
+        bpad     = 16;
105
+        break;
106
+    case PIX_FMT_RGB555LE:
107
+    case PIX_FMT_RGB555BE:
108
+    case PIX_FMT_BGR555LE:
109
+    case PIX_FMT_BGR555BE:
110
+        if (pix_fmt == PIX_FMT_BGR555LE ||
111
+            pix_fmt == PIX_FMT_BGR555BE) {
112
+            rgb[0] = 0x1F;
113
+            rgb[1] = 0x3E0;
114
+            rgb[2] = 0x7C00;
115
+        } else {
116
+            rgb[0] = 0x7C00;
117
+            rgb[1] = 0x3E0;
118
+            rgb[2] = 0x1F;
119
+        }
120
+        bpp      = 16;
121
+        vclass   = XWD_TRUE_COLOR;
122
+        bpad     = 16;
123
+        break;
124
+    case PIX_FMT_RGB8:
125
+    case PIX_FMT_BGR8:
126
+    case PIX_FMT_RGB4_BYTE:
127
+    case PIX_FMT_BGR4_BYTE:
128
+    case PIX_FMT_PAL8:
129
+        bpp      = 8;
130
+        vclass   = XWD_PSEUDO_COLOR;
131
+        bpad     = 8;
132
+        ncolors  = 256;
133
+        break;
134
+    case PIX_FMT_MONOWHITE:
135
+        bpp      = 1;
136
+        bpad     = 8;
137
+        vclass   = XWD_STATIC_GRAY;
138
+        break;
139
+    default:
140
+        av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
141
+        return AVERROR(EINVAL);
142
+    }
143
+
144
+    lsize       = FFALIGN(bpp * avctx->width, bpad) / 8;
145
+    header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
146
+    out_size    = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
147
+
148
+    if (buf_size < out_size) {
149
+        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
150
+        return AVERROR(ENOMEM);
151
+    }
152
+
153
+    avctx->coded_frame->key_frame = 1;
154
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
155
+
156
+    bytestream_put_be32(&buf, header_size);
157
+    bytestream_put_be32(&buf, XWD_VERSION);   // file version
158
+    bytestream_put_be32(&buf, XWD_Z_PIXMAP);  // pixmap format
159
+    bytestream_put_be32(&buf, pixdepth);      // pixmap depth in pixels
160
+    bytestream_put_be32(&buf, avctx->width);  // pixmap width in pixels
161
+    bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
162
+    bytestream_put_be32(&buf, 0);             // bitmap x offset
163
+    bytestream_put_be32(&buf, be);            // byte order
164
+    bytestream_put_be32(&buf, 32);            // bitmap unit
165
+    bytestream_put_be32(&buf, be);            // bit-order of image data
166
+    bytestream_put_be32(&buf, bpad);          // bitmap scan-line pad in bits
167
+    bytestream_put_be32(&buf, bpp);           // bits per pixel
168
+    bytestream_put_be32(&buf, lsize);         // bytes per scan-line
169
+    bytestream_put_be32(&buf, vclass);        // visual class
170
+    bytestream_put_be32(&buf, rgb[0]);        // red mask
171
+    bytestream_put_be32(&buf, rgb[1]);        // green mask
172
+    bytestream_put_be32(&buf, rgb[2]);        // blue mask
173
+    bytestream_put_be32(&buf, 8);             // size of each bitmask in bits
174
+    bytestream_put_be32(&buf, ncolors);       // number of colors
175
+    bytestream_put_be32(&buf, ncolors);       // number of entries in color map
176
+    bytestream_put_be32(&buf, avctx->width);  // window width
177
+    bytestream_put_be32(&buf, avctx->height); // window height
178
+    bytestream_put_be32(&buf, 0);             // window upper left X coordinate
179
+    bytestream_put_be32(&buf, 0);             // window upper left Y coordinate
180
+    bytestream_put_be32(&buf, 0);             // window border width
181
+    bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
182
+
183
+    for (i = 0; i < ncolors; i++) {
184
+        uint32_t val;
185
+        uint8_t red, green, blue;
186
+
187
+        val   = AV_RN32A(p->data[1] + i * 4);
188
+        red   = (val >> 16) & 0xFF;
189
+        green = (val >>  8) & 0xFF;
190
+        blue  =  val        & 0xFF;
191
+
192
+        bytestream_put_be32(&buf, i);         // colormap entry number
193
+        bytestream_put_be16(&buf, red   << 8);
194
+        bytestream_put_be16(&buf, green << 8);
195
+        bytestream_put_be16(&buf, blue  << 8);
196
+        bytestream_put_byte(&buf, 0x7);       // bitmask flag
197
+        bytestream_put_byte(&buf, 0);         // padding
198
+    }
199
+
200
+    ptr = p->data[0];
201
+    for (i = 0; i < avctx->height; i++) {
202
+        bytestream_put_buffer(&buf, ptr, lsize);
203
+        ptr += p->linesize[0];
204
+    }
205
+
206
+    return out_size;
207
+}
208
+
209
+static av_cold int xwd_encode_close(AVCodecContext *avctx)
210
+{
211
+    av_freep(&avctx->coded_frame);
212
+
213
+    return 0;
214
+}
215
+
216
+AVCodec ff_xwd_encoder = {
217
+    .name         = "xwd",
218
+    .type         = AVMEDIA_TYPE_VIDEO,
219
+    .id           = CODEC_ID_XWD,
220
+    .init         = xwd_encode_init,
221
+    .encode       = xwd_encode_frame,
222
+    .close        = xwd_encode_close,
223
+    .pix_fmts     = (const enum PixelFormat[]) { PIX_FMT_BGRA,
224
+                                                 PIX_FMT_RGBA,
225
+                                                 PIX_FMT_ARGB,
226
+                                                 PIX_FMT_ABGR,
227
+                                                 PIX_FMT_RGB24,
228
+                                                 PIX_FMT_BGR24,
229
+                                                 PIX_FMT_RGB565BE,
230
+                                                 PIX_FMT_RGB565LE,
231
+                                                 PIX_FMT_BGR565BE,
232
+                                                 PIX_FMT_BGR565LE,
233
+                                                 PIX_FMT_RGB555BE,
234
+                                                 PIX_FMT_RGB555LE,
235
+                                                 PIX_FMT_BGR555BE,
236
+                                                 PIX_FMT_BGR555LE,
237
+                                                 PIX_FMT_RGB8,
238
+                                                 PIX_FMT_BGR8,
239
+                                                 PIX_FMT_RGB4_BYTE,
240
+                                                 PIX_FMT_BGR4_BYTE,
241
+                                                 PIX_FMT_PAL8,
242
+                                                 PIX_FMT_MONOWHITE,
243
+                                                 PIX_FMT_NONE },
244
+    .long_name    = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
245
+};
... ...
@@ -326,8 +326,8 @@ OBJS-$(CONFIG_WC3_DEMUXER)               += wc3movie.o
326 326
 OBJS-$(CONFIG_WEBM_MUXER)                += matroskaenc.o matroska.o \
327 327
                                             riff.o isom.o avc.o \
328 328
                                             flacenc_header.o avlanguage.o
329
-OBJS-$(CONFIG_WSAUD_DEMUXER)             += westwood.o
330
-OBJS-$(CONFIG_WSVQA_DEMUXER)             += westwood.o
329
+OBJS-$(CONFIG_WSAUD_DEMUXER)             += westwood_aud.o
330
+OBJS-$(CONFIG_WSVQA_DEMUXER)             += westwood_vqa.o
331 331
 OBJS-$(CONFIG_WTV_DEMUXER)               += wtvdec.o wtv.o asfdec.o asf.o asfcrypt.o \
332 332
                                             avlanguage.o mpegts.o isom.o riff.o
333 333
 OBJS-$(CONFIG_WTV_MUXER)                 += wtvenc.o wtv.o asf.o asfenc.o riff.o
... ...
@@ -91,6 +91,7 @@ static const IdStrMap img_tags[] = {
91 91
     { CODEC_ID_JPEG2000  , "jpc"},
92 92
     { CODEC_ID_DPX       , "dpx"},
93 93
     { CODEC_ID_PICTOR    , "pic"},
94
+    { CODEC_ID_XWD       , "xwd"},
94 95
     { CODEC_ID_NONE      , NULL}
95 96
 };
96 97
 
... ...
@@ -528,7 +529,7 @@ AVOutputFormat ff_image2_muxer = {
528 528
     .name           = "image2",
529 529
     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
530 530
     .extensions     = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
531
-                      "ppm,sgi,tga,tif,tiff,jp2,j2c",
531
+                      "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd",
532 532
     .priv_data_size = sizeof(VideoData),
533 533
     .video_codec    = CODEC_ID_MJPEG,
534 534
     .write_header   = write_header,
535 535
deleted file mode 100644
... ...
@@ -1,389 +0,0 @@
1
-/*
2
- * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
3
- * Copyright (c) 2003 The ffmpeg Project
4
- *
5
- * This file is part of FFmpeg.
6
- *
7
- * FFmpeg is free software; you can redistribute it and/or
8
- * modify it under the terms of the GNU Lesser General Public
9
- * License as published by the Free Software Foundation; either
10
- * version 2.1 of the License, or (at your option) any later version.
11
- *
12
- * FFmpeg is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
- * Lesser General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU Lesser General Public
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
20
- */
21
-
22
-/**
23
- * @file
24
- * Westwood Studios VQA & AUD file demuxers
25
- * by Mike Melanson (melanson@pcisys.net)
26
- * for more information on the Westwood file formats, visit:
27
- *   http://www.pcisys.net/~melanson/codecs/
28
- *   http://www.geocities.com/SiliconValley/8682/aud3.txt
29
- *
30
- * Implementation note: There is no definite file signature for AUD files.
31
- * The demuxer uses a probabilistic strategy for content detection. This
32
- * entails performing sanity checks on certain header values in order to
33
- * qualify a file. Refer to wsaud_probe() for the precise parameters.
34
- */
35
-
36
-#include "libavutil/intreadwrite.h"
37
-#include "avformat.h"
38
-#include "internal.h"
39
-
40
-#define AUD_HEADER_SIZE 12
41
-#define AUD_CHUNK_PREAMBLE_SIZE 8
42
-#define AUD_CHUNK_SIGNATURE 0x0000DEAF
43
-
44
-#define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
45
-#define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
46
-#define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
47
-#define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
48
-#define SND0_TAG MKBETAG('S', 'N', 'D', '0')
49
-#define SND1_TAG MKBETAG('S', 'N', 'D', '1')
50
-#define SND2_TAG MKBETAG('S', 'N', 'D', '2')
51
-#define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
52
-
53
-/* don't know what these tags are for, but acknowledge their existence */
54
-#define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
55
-#define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
56
-#define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
57
-#define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
58
-#define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
59
-#define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
60
-#define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
61
-
62
-#define VQA_HEADER_SIZE 0x2A
63
-#define VQA_FRAMERATE 15
64
-#define VQA_PREAMBLE_SIZE 8
65
-
66
-typedef struct WsAudDemuxContext {
67
-    int audio_samplerate;
68
-    int audio_channels;
69
-    int audio_bits;
70
-    enum CodecID audio_type;
71
-    int audio_stream_index;
72
-    int64_t audio_frame_counter;
73
-} WsAudDemuxContext;
74
-
75
-typedef struct WsVqaDemuxContext {
76
-    int audio_samplerate;
77
-    int audio_channels;
78
-    int audio_bits;
79
-
80
-    int audio_stream_index;
81
-    int video_stream_index;
82
-
83
-    int64_t audio_frame_counter;
84
-} WsVqaDemuxContext;
85
-
86
-static int wsaud_probe(AVProbeData *p)
87
-{
88
-    int field;
89
-
90
-    /* Probabilistic content detection strategy: There is no file signature
91
-     * so perform sanity checks on various header parameters:
92
-     *   8000 <= sample rate (16 bits) <= 48000  ==> 40001 acceptable numbers
93
-     *   flags <= 0x03 (2 LSBs are used)         ==> 4 acceptable numbers
94
-     *   compression type (8 bits) = 1 or 99     ==> 2 acceptable numbers
95
-     *   first audio chunk signature (32 bits)   ==> 1 acceptable number
96
-     * The number space contains 2^64 numbers. There are 40001 * 4 * 2 * 1 =
97
-     * 320008 acceptable number combinations.
98
-     */
99
-
100
-    if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
101
-        return 0;
102
-
103
-    /* check sample rate */
104
-    field = AV_RL16(&p->buf[0]);
105
-    if ((field < 8000) || (field > 48000))
106
-        return 0;
107
-
108
-    /* enforce the rule that the top 6 bits of this flags field are reserved (0);
109
-     * this might not be true, but enforce it until deemed unnecessary */
110
-    if (p->buf[10] & 0xFC)
111
-        return 0;
112
-
113
-    /* note: only check for WS IMA (type 99) right now since there is no
114
-     * support for type 1 */
115
-    if (p->buf[11] != 99)
116
-        return 0;
117
-
118
-    /* read ahead to the first audio chunk and validate the first header signature */
119
-    if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
120
-        return 0;
121
-
122
-    /* return 1/2 certainty since this file check is a little sketchy */
123
-    return AVPROBE_SCORE_MAX / 2;
124
-}
125
-
126
-static int wsaud_read_header(AVFormatContext *s,
127
-                             AVFormatParameters *ap)
128
-{
129
-    WsAudDemuxContext *wsaud = s->priv_data;
130
-    AVIOContext *pb = s->pb;
131
-    AVStream *st;
132
-    unsigned char header[AUD_HEADER_SIZE];
133
-
134
-    if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
135
-        return AVERROR(EIO);
136
-    wsaud->audio_samplerate = AV_RL16(&header[0]);
137
-    if (header[11] == 99)
138
-        wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
139
-    else
140
-        return AVERROR_INVALIDDATA;
141
-
142
-    /* flag 0 indicates stereo */
143
-    wsaud->audio_channels = (header[10] & 0x1) + 1;
144
-    /* flag 1 indicates 16 bit audio */
145
-    wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
146
-
147
-    /* initialize the audio decoder stream */
148
-    st = avformat_new_stream(s, NULL);
149
-    if (!st)
150
-        return AVERROR(ENOMEM);
151
-    avpriv_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
152
-    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
153
-    st->codec->codec_id = wsaud->audio_type;
154
-    st->codec->codec_tag = 0;  /* no tag */
155
-    st->codec->channels = wsaud->audio_channels;
156
-    st->codec->sample_rate = wsaud->audio_samplerate;
157
-    st->codec->bits_per_coded_sample = wsaud->audio_bits;
158
-    st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
159
-        st->codec->bits_per_coded_sample / 4;
160
-    st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
161
-
162
-    wsaud->audio_stream_index = st->index;
163
-    wsaud->audio_frame_counter = 0;
164
-
165
-    return 0;
166
-}
167
-
168
-static int wsaud_read_packet(AVFormatContext *s,
169
-                             AVPacket *pkt)
170
-{
171
-    WsAudDemuxContext *wsaud = s->priv_data;
172
-    AVIOContext *pb = s->pb;
173
-    unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
174
-    unsigned int chunk_size;
175
-    int ret = 0;
176
-
177
-    if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
178
-        AUD_CHUNK_PREAMBLE_SIZE)
179
-        return AVERROR(EIO);
180
-
181
-    /* validate the chunk */
182
-    if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
183
-        return AVERROR_INVALIDDATA;
184
-
185
-    chunk_size = AV_RL16(&preamble[0]);
186
-    ret= av_get_packet(pb, pkt, chunk_size);
187
-    if (ret != chunk_size)
188
-        return AVERROR(EIO);
189
-    pkt->stream_index = wsaud->audio_stream_index;
190
-    pkt->pts = wsaud->audio_frame_counter;
191
-    pkt->pts /= wsaud->audio_samplerate;
192
-
193
-    /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
194
-    wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
195
-
196
-    return ret;
197
-}
198
-
199
-static int wsvqa_probe(AVProbeData *p)
200
-{
201
-    /* need 12 bytes to qualify */
202
-    if (p->buf_size < 12)
203
-        return 0;
204
-
205
-    /* check for the VQA signatures */
206
-    if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
207
-        (AV_RB32(&p->buf[8]) != WVQA_TAG))
208
-        return 0;
209
-
210
-    return AVPROBE_SCORE_MAX;
211
-}
212
-
213
-static int wsvqa_read_header(AVFormatContext *s,
214
-                             AVFormatParameters *ap)
215
-{
216
-    WsVqaDemuxContext *wsvqa = s->priv_data;
217
-    AVIOContext *pb = s->pb;
218
-    AVStream *st;
219
-    unsigned char *header;
220
-    unsigned char scratch[VQA_PREAMBLE_SIZE];
221
-    unsigned int chunk_tag;
222
-    unsigned int chunk_size;
223
-
224
-    /* initialize the video decoder stream */
225
-    st = avformat_new_stream(s, NULL);
226
-    if (!st)
227
-        return AVERROR(ENOMEM);
228
-    avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
229
-    wsvqa->video_stream_index = st->index;
230
-    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
231
-    st->codec->codec_id = CODEC_ID_WS_VQA;
232
-    st->codec->codec_tag = 0;  /* no fourcc */
233
-
234
-    /* skip to the start of the VQA header */
235
-    avio_seek(pb, 20, SEEK_SET);
236
-
237
-    /* the VQA header needs to go to the decoder */
238
-    st->codec->extradata_size = VQA_HEADER_SIZE;
239
-    st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
240
-    header = (unsigned char *)st->codec->extradata;
241
-    if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
242
-        VQA_HEADER_SIZE) {
243
-        av_free(st->codec->extradata);
244
-        return AVERROR(EIO);
245
-    }
246
-    st->codec->width = AV_RL16(&header[6]);
247
-    st->codec->height = AV_RL16(&header[8]);
248
-
249
-    /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
250
-    if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
251
-        st = avformat_new_stream(s, NULL);
252
-        if (!st)
253
-            return AVERROR(ENOMEM);
254
-        avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
255
-        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
256
-        if (AV_RL16(&header[0]) == 1)
257
-            st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
258
-        else
259
-            st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
260
-        st->codec->codec_tag = 0;  /* no tag */
261
-        st->codec->sample_rate = AV_RL16(&header[24]);
262
-        if (!st->codec->sample_rate)
263
-            st->codec->sample_rate = 22050;
264
-        st->codec->channels = header[26];
265
-        if (!st->codec->channels)
266
-            st->codec->channels = 1;
267
-        st->codec->bits_per_coded_sample = 16;
268
-        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
269
-            st->codec->bits_per_coded_sample / 4;
270
-        st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
271
-
272
-        wsvqa->audio_stream_index = st->index;
273
-        wsvqa->audio_samplerate = st->codec->sample_rate;
274
-        wsvqa->audio_channels = st->codec->channels;
275
-        wsvqa->audio_frame_counter = 0;
276
-    }
277
-
278
-    /* there are 0 or more chunks before the FINF chunk; iterate until
279
-     * FINF has been skipped and the file will be ready to be demuxed */
280
-    do {
281
-        if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
282
-            return AVERROR(EIO);
283
-        chunk_tag = AV_RB32(&scratch[0]);
284
-        chunk_size = AV_RB32(&scratch[4]);
285
-
286
-        /* catch any unknown header tags, for curiousity */
287
-        switch (chunk_tag) {
288
-        case CINF_TAG:
289
-        case CINH_TAG:
290
-        case CIND_TAG:
291
-        case PINF_TAG:
292
-        case PINH_TAG:
293
-        case PIND_TAG:
294
-        case FINF_TAG:
295
-        case CMDS_TAG:
296
-            break;
297
-
298
-        default:
299
-            av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
300
-                scratch[0], scratch[1],
301
-                scratch[2], scratch[3]);
302
-            break;
303
-        }
304
-
305
-        avio_skip(pb, chunk_size);
306
-    } while (chunk_tag != FINF_TAG);
307
-
308
-    return 0;
309
-}
310
-
311
-static int wsvqa_read_packet(AVFormatContext *s,
312
-                             AVPacket *pkt)
313
-{
314
-    WsVqaDemuxContext *wsvqa = s->priv_data;
315
-    AVIOContext *pb = s->pb;
316
-    int ret = -1;
317
-    unsigned char preamble[VQA_PREAMBLE_SIZE];
318
-    unsigned int chunk_type;
319
-    unsigned int chunk_size;
320
-    int skip_byte;
321
-
322
-    while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
323
-        chunk_type = AV_RB32(&preamble[0]);
324
-        chunk_size = AV_RB32(&preamble[4]);
325
-
326
-        skip_byte = chunk_size & 0x01;
327
-
328
-        if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
329
-            av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
330
-            return AVERROR_INVALIDDATA;
331
-        }
332
-
333
-        if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
334
-
335
-            ret= av_get_packet(pb, pkt, chunk_size);
336
-            if (ret<0)
337
-                return AVERROR(EIO);
338
-
339
-            if (chunk_type == SND2_TAG) {
340
-                pkt->stream_index = wsvqa->audio_stream_index;
341
-                /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
342
-                wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
343
-            } else if(chunk_type == SND1_TAG) {
344
-                pkt->stream_index = wsvqa->audio_stream_index;
345
-                /* unpacked size is stored in header */
346
-                wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
347
-            } else {
348
-                pkt->stream_index = wsvqa->video_stream_index;
349
-            }
350
-            /* stay on 16-bit alignment */
351
-            if (skip_byte)
352
-                avio_skip(pb, 1);
353
-
354
-            return ret;
355
-        } else {
356
-            switch(chunk_type){
357
-            case CMDS_TAG:
358
-            case SND0_TAG:
359
-                break;
360
-            default:
361
-                av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
362
-            }
363
-            avio_skip(pb, chunk_size + skip_byte);
364
-        }
365
-    }
366
-
367
-    return ret;
368
-}
369
-
370
-#if CONFIG_WSAUD_DEMUXER
371
-AVInputFormat ff_wsaud_demuxer = {
372
-    .name           = "wsaud",
373
-    .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
374
-    .priv_data_size = sizeof(WsAudDemuxContext),
375
-    .read_probe     = wsaud_probe,
376
-    .read_header    = wsaud_read_header,
377
-    .read_packet    = wsaud_read_packet,
378
-};
379
-#endif
380
-#if CONFIG_WSVQA_DEMUXER
381
-AVInputFormat ff_wsvqa_demuxer = {
382
-    .name           = "wsvqa",
383
-    .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
384
-    .priv_data_size = sizeof(WsVqaDemuxContext),
385
-    .read_probe     = wsvqa_probe,
386
-    .read_header    = wsvqa_read_header,
387
-    .read_packet    = wsvqa_read_packet,
388
-};
389
-#endif
390 1
new file mode 100644
... ...
@@ -0,0 +1,173 @@
0
+/*
1
+ * Westwood Studios AUD Format Demuxer
2
+ * Copyright (c) 2003 The ffmpeg Project
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Westwood Studios AUD file demuxer
24
+ * by Mike Melanson (melanson@pcisys.net)
25
+ * for more information on the Westwood file formats, visit:
26
+ *   http://www.pcisys.net/~melanson/codecs/
27
+ *   http://www.geocities.com/SiliconValley/8682/aud3.txt
28
+ *
29
+ * Implementation note: There is no definite file signature for AUD files.
30
+ * The demuxer uses a probabilistic strategy for content detection. This
31
+ * entails performing sanity checks on certain header values in order to
32
+ * qualify a file. Refer to wsaud_probe() for the precise parameters.
33
+ */
34
+
35
+#include "libavutil/intreadwrite.h"
36
+#include "avformat.h"
37
+#include "internal.h"
38
+
39
+#define AUD_HEADER_SIZE 12
40
+#define AUD_CHUNK_PREAMBLE_SIZE 8
41
+#define AUD_CHUNK_SIGNATURE 0x0000DEAF
42
+
43
+typedef struct WsAudDemuxContext {
44
+    int audio_samplerate;
45
+    int audio_channels;
46
+    int audio_bits;
47
+    enum CodecID audio_type;
48
+    int audio_stream_index;
49
+    int64_t audio_frame_counter;
50
+} WsAudDemuxContext;
51
+
52
+static int wsaud_probe(AVProbeData *p)
53
+{
54
+    int field;
55
+
56
+    /* Probabilistic content detection strategy: There is no file signature
57
+     * so perform sanity checks on various header parameters:
58
+     *   8000 <= sample rate (16 bits) <= 48000  ==> 40001 acceptable numbers
59
+     *   flags <= 0x03 (2 LSBs are used)         ==> 4 acceptable numbers
60
+     *   compression type (8 bits) = 1 or 99     ==> 2 acceptable numbers
61
+     *   first audio chunk signature (32 bits)   ==> 1 acceptable number
62
+     * The number space contains 2^64 numbers. There are 40001 * 4 * 2 * 1 =
63
+     * 320008 acceptable number combinations.
64
+     */
65
+
66
+    if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
67
+        return 0;
68
+
69
+    /* check sample rate */
70
+    field = AV_RL16(&p->buf[0]);
71
+    if ((field < 8000) || (field > 48000))
72
+        return 0;
73
+
74
+    /* enforce the rule that the top 6 bits of this flags field are reserved (0);
75
+     * this might not be true, but enforce it until deemed unnecessary */
76
+    if (p->buf[10] & 0xFC)
77
+        return 0;
78
+
79
+    /* note: only check for WS IMA (type 99) right now since there is no
80
+     * support for type 1 */
81
+    if (p->buf[11] != 99)
82
+        return 0;
83
+
84
+    /* read ahead to the first audio chunk and validate the first header signature */
85
+    if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
86
+        return 0;
87
+
88
+    /* return 1/2 certainty since this file check is a little sketchy */
89
+    return AVPROBE_SCORE_MAX / 2;
90
+}
91
+
92
+static int wsaud_read_header(AVFormatContext *s,
93
+                             AVFormatParameters *ap)
94
+{
95
+    WsAudDemuxContext *wsaud = s->priv_data;
96
+    AVIOContext *pb = s->pb;
97
+    AVStream *st;
98
+    unsigned char header[AUD_HEADER_SIZE];
99
+
100
+    if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
101
+        return AVERROR(EIO);
102
+    wsaud->audio_samplerate = AV_RL16(&header[0]);
103
+    if (header[11] == 99)
104
+        wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
105
+    else
106
+        return AVERROR_INVALIDDATA;
107
+
108
+    /* flag 0 indicates stereo */
109
+    wsaud->audio_channels = (header[10] & 0x1) + 1;
110
+    /* flag 1 indicates 16 bit audio */
111
+    wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
112
+
113
+    /* initialize the audio decoder stream */
114
+    st = avformat_new_stream(s, NULL);
115
+    if (!st)
116
+        return AVERROR(ENOMEM);
117
+    avpriv_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
118
+    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
119
+    st->codec->codec_id = wsaud->audio_type;
120
+    st->codec->codec_tag = 0;  /* no tag */
121
+    st->codec->channels = wsaud->audio_channels;
122
+    st->codec->sample_rate = wsaud->audio_samplerate;
123
+    st->codec->bits_per_coded_sample = wsaud->audio_bits;
124
+    st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
125
+        st->codec->bits_per_coded_sample / 4;
126
+    st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
127
+
128
+    wsaud->audio_stream_index = st->index;
129
+    wsaud->audio_frame_counter = 0;
130
+
131
+    return 0;
132
+}
133
+
134
+static int wsaud_read_packet(AVFormatContext *s,
135
+                             AVPacket *pkt)
136
+{
137
+    WsAudDemuxContext *wsaud = s->priv_data;
138
+    AVIOContext *pb = s->pb;
139
+    unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
140
+    unsigned int chunk_size;
141
+    int ret = 0;
142
+
143
+    if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
144
+        AUD_CHUNK_PREAMBLE_SIZE)
145
+        return AVERROR(EIO);
146
+
147
+    /* validate the chunk */
148
+    if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
149
+        return AVERROR_INVALIDDATA;
150
+
151
+    chunk_size = AV_RL16(&preamble[0]);
152
+    ret= av_get_packet(pb, pkt, chunk_size);
153
+    if (ret != chunk_size)
154
+        return AVERROR(EIO);
155
+    pkt->stream_index = wsaud->audio_stream_index;
156
+    pkt->pts = wsaud->audio_frame_counter;
157
+    pkt->pts /= wsaud->audio_samplerate;
158
+
159
+    /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
160
+    wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
161
+
162
+    return ret;
163
+}
164
+
165
+AVInputFormat ff_wsaud_demuxer = {
166
+    .name           = "wsaud",
167
+    .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
168
+    .priv_data_size = sizeof(WsAudDemuxContext),
169
+    .read_probe     = wsaud_probe,
170
+    .read_header    = wsaud_read_header,
171
+    .read_packet    = wsaud_read_packet,
172
+};
0 173
new file mode 100644
... ...
@@ -0,0 +1,246 @@
0
+/*
1
+ * Westwood Studios VQA Format Demuxer
2
+ * Copyright (c) 2003 The ffmpeg Project
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg 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.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Westwood Studios VQA file demuxer
24
+ * by Mike Melanson (melanson@pcisys.net)
25
+ * for more information on the Westwood file formats, visit:
26
+ *   http://www.pcisys.net/~melanson/codecs/
27
+ *   http://www.geocities.com/SiliconValley/8682/aud3.txt
28
+ */
29
+
30
+#include "libavutil/intreadwrite.h"
31
+#include "avformat.h"
32
+#include "internal.h"
33
+
34
+#define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
35
+#define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
36
+#define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
37
+#define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
38
+#define SND0_TAG MKBETAG('S', 'N', 'D', '0')
39
+#define SND1_TAG MKBETAG('S', 'N', 'D', '1')
40
+#define SND2_TAG MKBETAG('S', 'N', 'D', '2')
41
+#define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
42
+
43
+/* don't know what these tags are for, but acknowledge their existence */
44
+#define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
45
+#define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
46
+#define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
47
+#define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
48
+#define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
49
+#define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
50
+#define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
51
+
52
+#define VQA_HEADER_SIZE 0x2A
53
+#define VQA_FRAMERATE 15
54
+#define VQA_PREAMBLE_SIZE 8
55
+
56
+typedef struct WsVqaDemuxContext {
57
+    int audio_samplerate;
58
+    int audio_channels;
59
+    int audio_bits;
60
+
61
+    int audio_stream_index;
62
+    int video_stream_index;
63
+
64
+    int64_t audio_frame_counter;
65
+} WsVqaDemuxContext;
66
+
67
+static int wsvqa_probe(AVProbeData *p)
68
+{
69
+    /* need 12 bytes to qualify */
70
+    if (p->buf_size < 12)
71
+        return 0;
72
+
73
+    /* check for the VQA signatures */
74
+    if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
75
+        (AV_RB32(&p->buf[8]) != WVQA_TAG))
76
+        return 0;
77
+
78
+    return AVPROBE_SCORE_MAX;
79
+}
80
+
81
+static int wsvqa_read_header(AVFormatContext *s,
82
+                             AVFormatParameters *ap)
83
+{
84
+    WsVqaDemuxContext *wsvqa = s->priv_data;
85
+    AVIOContext *pb = s->pb;
86
+    AVStream *st;
87
+    unsigned char *header;
88
+    unsigned char scratch[VQA_PREAMBLE_SIZE];
89
+    unsigned int chunk_tag;
90
+    unsigned int chunk_size;
91
+
92
+    /* initialize the video decoder stream */
93
+    st = avformat_new_stream(s, NULL);
94
+    if (!st)
95
+        return AVERROR(ENOMEM);
96
+    avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
97
+    wsvqa->video_stream_index = st->index;
98
+    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
99
+    st->codec->codec_id = CODEC_ID_WS_VQA;
100
+    st->codec->codec_tag = 0;  /* no fourcc */
101
+
102
+    /* skip to the start of the VQA header */
103
+    avio_seek(pb, 20, SEEK_SET);
104
+
105
+    /* the VQA header needs to go to the decoder */
106
+    st->codec->extradata_size = VQA_HEADER_SIZE;
107
+    st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
108
+    header = (unsigned char *)st->codec->extradata;
109
+    if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
110
+        VQA_HEADER_SIZE) {
111
+        av_free(st->codec->extradata);
112
+        return AVERROR(EIO);
113
+    }
114
+    st->codec->width = AV_RL16(&header[6]);
115
+    st->codec->height = AV_RL16(&header[8]);
116
+
117
+    /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
118
+    if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
119
+        st = avformat_new_stream(s, NULL);
120
+        if (!st)
121
+            return AVERROR(ENOMEM);
122
+        avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
123
+        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
124
+        if (AV_RL16(&header[0]) == 1)
125
+            st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
126
+        else
127
+            st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
128
+        st->codec->codec_tag = 0;  /* no tag */
129
+        st->codec->sample_rate = AV_RL16(&header[24]);
130
+        if (!st->codec->sample_rate)
131
+            st->codec->sample_rate = 22050;
132
+        st->codec->channels = header[26];
133
+        if (!st->codec->channels)
134
+            st->codec->channels = 1;
135
+        st->codec->bits_per_coded_sample = 16;
136
+        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
137
+            st->codec->bits_per_coded_sample / 4;
138
+        st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
139
+
140
+        wsvqa->audio_stream_index = st->index;
141
+        wsvqa->audio_samplerate = st->codec->sample_rate;
142
+        wsvqa->audio_channels = st->codec->channels;
143
+        wsvqa->audio_frame_counter = 0;
144
+    }
145
+
146
+    /* there are 0 or more chunks before the FINF chunk; iterate until
147
+     * FINF has been skipped and the file will be ready to be demuxed */
148
+    do {
149
+        if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
150
+            return AVERROR(EIO);
151
+        chunk_tag = AV_RB32(&scratch[0]);
152
+        chunk_size = AV_RB32(&scratch[4]);
153
+
154
+        /* catch any unknown header tags, for curiousity */
155
+        switch (chunk_tag) {
156
+        case CINF_TAG:
157
+        case CINH_TAG:
158
+        case CIND_TAG:
159
+        case PINF_TAG:
160
+        case PINH_TAG:
161
+        case PIND_TAG:
162
+        case FINF_TAG:
163
+        case CMDS_TAG:
164
+            break;
165
+
166
+        default:
167
+            av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
168
+                scratch[0], scratch[1],
169
+                scratch[2], scratch[3]);
170
+            break;
171
+        }
172
+
173
+        avio_skip(pb, chunk_size);
174
+    } while (chunk_tag != FINF_TAG);
175
+
176
+    return 0;
177
+}
178
+
179
+static int wsvqa_read_packet(AVFormatContext *s,
180
+                             AVPacket *pkt)
181
+{
182
+    WsVqaDemuxContext *wsvqa = s->priv_data;
183
+    AVIOContext *pb = s->pb;
184
+    int ret = -1;
185
+    unsigned char preamble[VQA_PREAMBLE_SIZE];
186
+    unsigned int chunk_type;
187
+    unsigned int chunk_size;
188
+    int skip_byte;
189
+
190
+    while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
191
+        chunk_type = AV_RB32(&preamble[0]);
192
+        chunk_size = AV_RB32(&preamble[4]);
193
+
194
+        skip_byte = chunk_size & 0x01;
195
+
196
+        if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
197
+            av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
198
+            return AVERROR_INVALIDDATA;
199
+        }
200
+
201
+        if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
202
+
203
+            ret= av_get_packet(pb, pkt, chunk_size);
204
+            if (ret<0)
205
+                return AVERROR(EIO);
206
+
207
+            if (chunk_type == SND2_TAG) {
208
+                pkt->stream_index = wsvqa->audio_stream_index;
209
+                /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
210
+                wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
211
+            } else if(chunk_type == SND1_TAG) {
212
+                pkt->stream_index = wsvqa->audio_stream_index;
213
+                /* unpacked size is stored in header */
214
+                wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
215
+            } else {
216
+                pkt->stream_index = wsvqa->video_stream_index;
217
+            }
218
+            /* stay on 16-bit alignment */
219
+            if (skip_byte)
220
+                avio_skip(pb, 1);
221
+
222
+            return ret;
223
+        } else {
224
+            switch(chunk_type){
225
+            case CMDS_TAG:
226
+            case SND0_TAG:
227
+                break;
228
+            default:
229
+                av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
230
+            }
231
+            avio_skip(pb, chunk_size + skip_byte);
232
+        }
233
+    }
234
+
235
+    return ret;
236
+}
237
+
238
+AVInputFormat ff_wsvqa_demuxer = {
239
+    .name           = "wsvqa",
240
+    .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
241
+    .priv_data_size = sizeof(WsVqaDemuxContext),
242
+    .read_probe     = wsvqa_probe,
243
+    .read_header    = wsvqa_read_header,
244
+    .read_packet    = wsvqa_read_packet,
245
+};