~ubuntu-branches/ubuntu/saucy/gst-libav1.0/saucy-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/wmaprodec.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-07-30 09:00:15 UTC
  • mfrom: (1.1.16) (7.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20130730090015-sc1ou2yssu7q5w4e
Tags: 1.1.3-1
* New upstream development snapshot:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.1.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
 * subframe in order to reconstruct the output samples.
87
87
 */
88
88
 
 
89
#include "libavutil/float_dsp.h"
89
90
#include "libavutil/intfloat.h"
90
91
#include "libavutil/intreadwrite.h"
91
92
#include "avcodec.h"
94
95
#include "put_bits.h"
95
96
#include "wmaprodata.h"
96
97
#include "dsputil.h"
97
 
#include "fmtconvert.h"
98
98
#include "sinewin.h"
99
99
#include "wma.h"
 
100
#include "wma_common.h"
100
101
 
101
102
/** current decoder limitations */
102
103
#define WMAPRO_MAX_CHANNELS    8                             ///< max number of handled channels
105
106
#define MAX_FRAMESIZE  32768                                 ///< maximum compressed frame size
106
107
 
107
108
#define WMAPRO_BLOCK_MIN_BITS  6                                           ///< log2 of min block size
108
 
#define WMAPRO_BLOCK_MAX_BITS 12                                           ///< log2 of max block size
 
109
#define WMAPRO_BLOCK_MAX_BITS 13                                           ///< log2 of max block size
109
110
#define WMAPRO_BLOCK_MIN_SIZE (1 << WMAPRO_BLOCK_MIN_BITS)                 ///< minimum block size
110
111
#define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)                 ///< maximum block size
111
112
#define WMAPRO_BLOCK_SIZES    (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1) ///< possible block sizes
171
172
    AVCodecContext*  avctx;                         ///< codec context for av_log
172
173
    AVFrame          frame;                         ///< AVFrame for decoded output
173
174
    DSPContext       dsp;                           ///< accelerated DSP functions
174
 
    FmtConvertContext fmt_conv;
 
175
    AVFloatDSPContext fdsp;
175
176
    uint8_t          frame_data[MAX_FRAMESIZE +
176
177
                      FF_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
177
178
    PutBitContext    pb;                            ///< context for filling the frame_data buffer
186
187
    uint8_t          bits_per_sample;               ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
187
188
    uint16_t         samples_per_frame;             ///< number of samples to output
188
189
    uint16_t         log2_frame_size;
189
 
    int8_t           num_channels;                  ///< number of channels in the stream (same as AVCodecContext.num_channels)
190
190
    int8_t           lfe_channel;                   ///< lfe channel index
191
191
    uint8_t          max_num_subframes;
192
192
    uint8_t          subframe_len_bits;             ///< number of bits used for the subframe length
237
237
 *@brief helper function to print the most important members of the context
238
238
 *@param s context
239
239
 */
240
 
static void av_cold dump_context(WMAProDecodeCtx *s)
 
240
static av_cold void dump_context(WMAProDecodeCtx *s)
241
241
{
242
242
#define PRINT(a, b)     av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
243
243
#define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
248
248
    PRINT("log2 frame size",     s->log2_frame_size);
249
249
    PRINT("max num subframes",   s->max_num_subframes);
250
250
    PRINT("len prefix",          s->len_prefix);
251
 
    PRINT("num channels",        s->num_channels);
 
251
    PRINT("num channels",        s->avctx->channels);
252
252
}
253
253
 
254
254
/**
277
277
    WMAProDecodeCtx *s = avctx->priv_data;
278
278
    uint8_t *edata_ptr = avctx->extradata;
279
279
    unsigned int channel_mask;
280
 
    int i;
 
280
    int i, bits;
281
281
    int log2_max_num_subframes;
282
282
    int num_possible_block_sizes;
283
283
 
287
287
    }
288
288
 
289
289
    s->avctx = avctx;
290
 
    dsputil_init(&s->dsp, avctx);
291
 
    ff_fmt_convert_init(&s->fmt_conv, avctx);
 
290
    ff_dsputil_init(&s->dsp, avctx);
 
291
    avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
 
292
 
292
293
    init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
293
294
 
294
 
    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
 
295
    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
295
296
 
296
297
    if (avctx->extradata_size >= 18) {
297
298
        s->decode_flags    = AV_RL16(edata_ptr+14);
304
305
 
305
306
    } else {
306
307
        av_log_ask_for_sample(avctx, "Unknown extradata size\n");
307
 
        return AVERROR_INVALIDDATA;
 
308
        return AVERROR_PATCHWELCOME;
308
309
    }
309
310
 
310
311
    /** generic init */
316
317
    s->len_prefix  = (s->decode_flags & 0x40);
317
318
 
318
319
    /** get frame len */
319
 
    s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
320
 
                                                          3, s->decode_flags);
 
320
    bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
 
321
    if (bits > WMAPRO_BLOCK_MAX_BITS) {
 
322
        av_log_missing_feature(avctx, "14-bits block sizes", 1);
 
323
        return AVERROR_PATCHWELCOME;
 
324
    }
 
325
    s->samples_per_frame = 1 << bits;
321
326
 
322
327
    /** subframe info */
323
328
    log2_max_num_subframes       = ((s->decode_flags & 0x38) >> 3);
347
352
        return AVERROR_INVALIDDATA;
348
353
    }
349
354
 
350
 
    s->num_channels = avctx->channels;
351
 
 
352
 
    if (s->num_channels < 0) {
353
 
        av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
 
355
    if (avctx->channels < 0) {
 
356
        av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
 
357
               avctx->channels);
354
358
        return AVERROR_INVALIDDATA;
355
 
    } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
 
359
    } else if (avctx->channels > WMAPRO_MAX_CHANNELS) {
356
360
        av_log_ask_for_sample(avctx, "unsupported number of channels\n");
357
361
        return AVERROR_PATCHWELCOME;
358
362
    }
359
363
 
360
364
    /** init previous block len */
361
 
    for (i = 0; i < s->num_channels; i++)
 
365
    for (i = 0; i < avctx->channels; i++)
362
366
        s->channel[i].prev_block_len = s->samples_per_frame;
363
367
 
364
368
    /** extract lfe channel position */
534
538
 */
535
539
static int decode_tilehdr(WMAProDecodeCtx *s)
536
540
{
537
 
    uint16_t num_samples[WMAPRO_MAX_CHANNELS];        /**< sum of samples for all currently known subframes of a channel */
 
541
    uint16_t num_samples[WMAPRO_MAX_CHANNELS] = { 0 };/**< sum of samples for all currently known subframes of a channel */
538
542
    uint8_t  contains_subframe[WMAPRO_MAX_CHANNELS];  /**< flag indicating if a channel contains the current subframe */
539
 
    int channels_for_cur_subframe = s->num_channels;  /**< number of channels that contain the current subframe */
 
543
    int channels_for_cur_subframe = s->avctx->channels; /**< number of channels that contain the current subframe */
540
544
    int fixed_channel_layout = 0;                     /**< flag indicating that all channels use the same subframe offsets and sizes */
541
545
    int min_channel_len = 0;                          /**< smallest sum of samples (channels with this length will be processed first) */
542
546
    int c;
543
547
 
544
548
    /* Should never consume more than 3073 bits (256 iterations for the
545
 
     * while loop when always the minimum amount of 128 samples is substracted
 
549
     * while loop when always the minimum amount of 128 samples is subtracted
546
550
     * from missing samples in the 8 channel case).
547
551
     * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS  + 4)
548
552
     */
549
553
 
550
554
    /** reset tiling information */
551
 
    for (c = 0; c < s->num_channels; c++)
 
555
    for (c = 0; c < s->avctx->channels; c++)
552
556
        s->channel[c].num_subframes = 0;
553
557
 
554
 
    memset(num_samples, 0, sizeof(num_samples));
555
 
 
556
558
    if (s->max_num_subframes == 1 || get_bits1(&s->gb))
557
559
        fixed_channel_layout = 1;
558
560
 
561
563
        int subframe_len;
562
564
 
563
565
        /** check which channels contain the subframe */
564
 
        for (c = 0; c < s->num_channels; c++) {
 
566
        for (c = 0; c < s->avctx->channels; c++) {
565
567
            if (num_samples[c] == min_channel_len) {
566
568
                if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
567
569
                   (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
578
580
 
579
581
        /** add subframes to the individual channels and find new min_channel_len */
580
582
        min_channel_len += subframe_len;
581
 
        for (c = 0; c < s->num_channels; c++) {
 
583
        for (c = 0; c < s->avctx->channels; c++) {
582
584
            WMAProChannelCtx* chan = &s->channel[c];
583
585
 
584
586
            if (contains_subframe[c]) {
605
607
        }
606
608
    } while (min_channel_len < s->samples_per_frame);
607
609
 
608
 
    for (c = 0; c < s->num_channels; c++) {
 
610
    for (c = 0; c < s->avctx->channels; c++) {
609
611
        int i;
610
612
        int offset = 0;
611
613
        for (i = 0; i < s->channel[c].num_subframes; i++) {
631
633
    int i;
632
634
    int offset = 0;
633
635
    int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
634
 
    memset(chgroup->decorrelation_matrix, 0, s->num_channels *
635
 
           s->num_channels * sizeof(*chgroup->decorrelation_matrix));
 
636
    memset(chgroup->decorrelation_matrix, 0, s->avctx->channels *
 
637
           s->avctx->channels * sizeof(*chgroup->decorrelation_matrix));
636
638
 
637
639
    for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
638
640
        rotation_offset[i] = get_bits(&s->gb, 6);
685
687
 
686
688
    /** in the one channel case channel transforms are pointless */
687
689
    s->num_chgroups = 0;
688
 
    if (s->num_channels > 1) {
 
690
    if (s->avctx->channels > 1) {
689
691
        int remaining_channels = s->channels_for_cur_subframe;
690
692
 
691
693
        if (get_bits1(&s->gb)) {
692
694
            av_log_ask_for_sample(s->avctx,
693
695
                                  "unsupported channel transform bit\n");
694
 
            return AVERROR_INVALIDDATA;
 
696
            return AVERROR_PATCHWELCOME;
695
697
        }
696
698
 
697
699
        for (s->num_chgroups = 0; remaining_channels &&
732
734
                    }
733
735
                } else {
734
736
                    chgroup->transform = 1;
735
 
                    if (s->num_channels == 2) {
 
737
                    if (s->avctx->channels == 2) {
736
738
                        chgroup->decorrelation_matrix[0] =  1.0;
737
739
                        chgroup->decorrelation_matrix[1] = -1.0;
738
740
                        chgroup->decorrelation_matrix[2] =  1.0;
1022
1024
                            (*ch)[y] = sum;
1023
1025
                        }
1024
1026
                    }
1025
 
                } else if (s->num_channels == 2) {
 
1027
                } else if (s->avctx->channels == 2) {
1026
1028
                    int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
1027
 
                    s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
1028
 
                                              ch_data[0] + sfb[0],
1029
 
                                              181.0 / 128, len);
1030
 
                    s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
1031
 
                                              ch_data[1] + sfb[0],
1032
 
                                              181.0 / 128, len);
 
1029
                    s->fdsp.vector_fmul_scalar(ch_data[0] + sfb[0],
 
1030
                                               ch_data[0] + sfb[0],
 
1031
                                               181.0 / 128, len);
 
1032
                    s->fdsp.vector_fmul_scalar(ch_data[1] + sfb[0],
 
1033
                                               ch_data[1] + sfb[0],
 
1034
                                               181.0 / 128, len);
1033
1035
                }
1034
1036
            }
1035
1037
        }
1075
1077
    int offset = s->samples_per_frame;
1076
1078
    int subframe_len = s->samples_per_frame;
1077
1079
    int i;
1078
 
    int total_samples   = s->samples_per_frame * s->num_channels;
 
1080
    int total_samples   = s->samples_per_frame * s->avctx->channels;
1079
1081
    int transmit_coeffs = 0;
1080
1082
    int cur_subwoofer_cutoff;
1081
1083
 
1085
1087
        == the next block of the channel with the smallest number of
1086
1088
        decoded samples
1087
1089
    */
1088
 
    for (i = 0; i < s->num_channels; i++) {
 
1090
    for (i = 0; i < s->avctx->channels; i++) {
1089
1091
        s->channel[i].grouped = 0;
1090
1092
        if (offset > s->channel[i].decoded_samples) {
1091
1093
            offset = s->channel[i].decoded_samples;
1099
1101
 
1100
1102
    /** get a list of all channels that contain the estimated block */
1101
1103
    s->channels_for_cur_subframe = 0;
1102
 
    for (i = 0; i < s->num_channels; i++) {
 
1104
    for (i = 0; i < s->avctx->channels; i++) {
1103
1105
        const int cur_subframe = s->channel[i].cur_subframe;
1104
 
        /** substract already processed samples */
 
1106
        /** subtract already processed samples */
1105
1107
        total_samples -= s->channel[i].decoded_samples;
1106
1108
 
1107
1109
        /** and count if there are multiple subframes that match our profile */
1163
1165
    /** no idea for what the following bit is used */
1164
1166
    if (get_bits1(&s->gb)) {
1165
1167
        av_log_ask_for_sample(s->avctx, "reserved bit set\n");
1166
 
        return AVERROR_INVALIDDATA;
 
1168
        return AVERROR_PATCHWELCOME;
1167
1169
    }
1168
1170
 
1169
1171
 
1276
1278
                            s->channel[c].scale_factor_step;
1277
1279
                const float quant = pow(10.0, exp / 20.0);
1278
1280
                int start = s->cur_sfb_offsets[b];
1279
 
                s->dsp.vector_fmul_scalar(s->tmp + start,
1280
 
                                          s->channel[c].coeffs + start,
1281
 
                                          quant, end - start);
 
1281
                s->fdsp.vector_fmul_scalar(s->tmp + start,
 
1282
                                           s->channel[c].coeffs + start,
 
1283
                                           quant, end - start);
1282
1284
            }
1283
1285
 
1284
1286
            /** apply imdct (imdct_half == DCTIV with reverse) */
1315
1317
    int more_frames = 0;
1316
1318
    int len = 0;
1317
1319
    int i, ret;
1318
 
    const float *out_ptr[WMAPRO_MAX_CHANNELS];
1319
 
    float *samples;
1320
1320
 
1321
1321
    /** get frame length */
1322
1322
    if (s->len_prefix)
1331
1331
    }
1332
1332
 
1333
1333
    /** read postproc transform */
1334
 
    if (s->num_channels > 1 && get_bits1(gb)) {
 
1334
    if (s->avctx->channels > 1 && get_bits1(gb)) {
1335
1335
        if (get_bits1(gb)) {
1336
 
            for (i = 0; i < s->num_channels * s->num_channels; i++)
 
1336
            for (i = 0; i < avctx->channels * avctx->channels; i++)
1337
1337
                skip_bits(gb, 4);
1338
1338
        }
1339
1339
    }
1368
1368
 
1369
1369
    /** reset subframe states */
1370
1370
    s->parsed_all_subframes = 0;
1371
 
    for (i = 0; i < s->num_channels; i++) {
 
1371
    for (i = 0; i < avctx->channels; i++) {
1372
1372
        s->channel[i].decoded_samples = 0;
1373
1373
        s->channel[i].cur_subframe    = 0;
1374
1374
        s->channel[i].reuse_sf        = 0;
1384
1384
 
1385
1385
    /* get output buffer */
1386
1386
    s->frame.nb_samples = s->samples_per_frame;
1387
 
    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
 
1387
    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
1388
1388
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1389
1389
        s->packet_loss = 1;
1390
1390
        return 0;
1391
1391
    }
1392
 
    samples = (float *)s->frame.data[0];
1393
 
 
1394
 
    /** interleave samples and write them to the output buffer */
1395
 
    for (i = 0; i < s->num_channels; i++)
1396
 
        out_ptr[i] = s->channel[i].out;
1397
 
    s->fmt_conv.float_interleave(samples, out_ptr, s->samples_per_frame,
1398
 
                                 s->num_channels);
1399
 
 
1400
 
    for (i = 0; i < s->num_channels; i++) {
 
1392
 
 
1393
    /** copy samples to the output buffer */
 
1394
    for (i = 0; i < avctx->channels; i++)
 
1395
        memcpy(s->frame.extended_data[i], s->channel[i].out,
 
1396
               s->samples_per_frame * sizeof(*s->channel[i].out));
 
1397
 
 
1398
    for (i = 0; i < avctx->channels; i++) {
1401
1399
        /** reuse second half of the IMDCT output for the next frame */
1402
1400
        memcpy(&s->channel[i].out[0],
1403
1401
               &s->channel[i].out[s->samples_per_frame],
1510
1508
 *@brief Decode a single WMA packet.
1511
1509
 *@param avctx codec context
1512
1510
 *@param data the output buffer
1513
 
 *@param data_size number of bytes that were written to the output buffer
1514
1511
 *@param avpkt input packet
1515
1512
 *@return number of bytes that were read from the input buffer
1516
1513
 */
1639
1636
    int i;
1640
1637
    /** reset output buffer as a part of it is used during the windowing of a
1641
1638
        new frame */
1642
 
    for (i = 0; i < s->num_channels; i++)
 
1639
    for (i = 0; i < avctx->channels; i++)
1643
1640
        memset(s->channel[i].out, 0, s->samples_per_frame *
1644
1641
               sizeof(*s->channel[i].out));
1645
1642
    s->packet_loss = 1;
1652
1649
AVCodec ff_wmapro_decoder = {
1653
1650
    .name           = "wmapro",
1654
1651
    .type           = AVMEDIA_TYPE_AUDIO,
1655
 
    .id             = CODEC_ID_WMAPRO,
 
1652
    .id             = AV_CODEC_ID_WMAPRO,
1656
1653
    .priv_data_size = sizeof(WMAProDecodeCtx),
1657
1654
    .init           = decode_init,
1658
1655
    .close          = decode_end,
1659
1656
    .decode         = decode_packet,
1660
1657
    .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1661
 
    .flush= flush,
1662
 
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
 
1658
    .flush          = flush,
 
1659
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
 
1660
    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
 
1661
                                                      AV_SAMPLE_FMT_NONE },
1663
1662
};