~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/wmavoice.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 * @author Ronald S. Bultje <rsbultje@gmail.com>
26
26
 */
27
27
 
 
28
#define UNCHECKED_BITSTREAM_READER 1
 
29
 
28
30
#include <math.h>
29
31
#include "avcodec.h"
30
32
#include "get_bits.h"
128
130
 */
129
131
typedef struct {
130
132
    /**
131
 
     * @defgroup struct_global Global values
132
 
     * Global values, specified in the stream header / extradata or used
133
 
     * all over.
 
133
     * @name Global values specified in the stream header / extradata or used all over.
134
134
     * @{
135
135
     */
 
136
    AVFrame frame;
136
137
    GetBitContext gb;             ///< packet bitreader. During decoder init,
137
138
                                  ///< it contains the extradata from the
138
139
                                  ///< demuxer. During decoding, it contains
182
183
 
183
184
    /**
184
185
     * @}
185
 
     * @defgroup struct_packet Packet values
186
 
     * Packet values, specified in the packet header or related to a packet.
 
186
     *
 
187
     * @name Packet values specified in the packet header or related to a packet.
 
188
     *
187
189
     * A packet is considered to be a single unit of data provided to this
188
190
     * decoder by the demuxer.
189
191
     * @{
190
192
     */
191
193
    int spillover_nbits;          ///< number of bits of the previous packet's
192
 
                                  ///< last superframe preceeding this
 
194
                                  ///< last superframe preceding this
193
195
                                  ///< packet's first full superframe (useful
194
196
                                  ///< for re-synchronization also)
195
197
    int has_residual_lsps;        ///< if set, superframes contain one set of
213
215
 
214
216
    /**
215
217
     * @}
216
 
     * @defgroup struct_frame Frame and superframe values
 
218
     *
 
219
     * @name Frame and superframe values
217
220
     * Superframe and frame data - these can change from frame to frame,
218
221
     * although some of them do in that case serve as a cache / history for
219
222
     * the next frame or superframe.
256
259
    float synth_history[MAX_LSPS]; ///< see #excitation_history
257
260
    /**
258
261
     * @}
259
 
     * @defgroup post_filter Postfilter values
 
262
     *
 
263
     * @name Postfilter values
 
264
     *
260
265
     * Variables used for postfilter implementation, mostly history for
261
266
     * smoothing and so on, and context variables for FFT/iFFT.
262
267
     * @{
399
404
    s->min_pitch_val    = ((ctx->sample_rate << 8)      /  400 + 50) >> 8;
400
405
    s->max_pitch_val    = ((ctx->sample_rate << 8) * 37 / 2000 + 50) >> 8;
401
406
    pitch_range         = s->max_pitch_val - s->min_pitch_val;
 
407
    if (pitch_range <= 0) {
 
408
        av_log(ctx, AV_LOG_ERROR, "Invalid pitch range; broken extradata?\n");
 
409
        return -1;
 
410
    }
402
411
    s->pitch_nbits      = av_ceil_log2(pitch_range);
403
412
    s->last_pitch_val   = 40;
404
413
    s->last_acb_type    = ACB_TYPE_NONE;
420
429
    s->block_conv_table[2]      = (pitch_range * 44) >> 6;
421
430
    s->block_conv_table[3]      = s->max_pitch_val - 1;
422
431
    s->block_delta_pitch_hrange = (pitch_range >> 3) & ~0xF;
 
432
    if (s->block_delta_pitch_hrange <= 0) {
 
433
        av_log(ctx, AV_LOG_ERROR, "Invalid delta pitch hrange; broken extradata?\n");
 
434
        return -1;
 
435
    }
423
436
    s->block_delta_pitch_nbits  = 1 + av_ceil_log2(s->block_delta_pitch_hrange);
424
437
    s->block_pitch_range        = s->block_conv_table[2] +
425
438
                                  s->block_conv_table[3] + 1 +
428
441
 
429
442
    ctx->sample_fmt             = AV_SAMPLE_FMT_FLT;
430
443
 
 
444
    avcodec_get_frame_defaults(&s->frame);
 
445
    ctx->coded_frame = &s->frame;
 
446
 
431
447
    return 0;
432
448
}
433
449
 
434
450
/**
435
 
 * @defgroup postfilter Postfilter functions
 
451
 * @name Postfilter functions
436
452
 * Postfilter functions (gain control, wiener denoise filter, DC filter,
437
453
 * kalman smoothening, plus surrounding code to wrap it)
438
454
 * @{
825
841
}
826
842
 
827
843
/**
828
 
 * @defgroup lsp_dequant LSP dequantization routines
 
844
 * @name LSP dequantization routines
829
845
 * LSP dequantization routines, for 10/16LSPs and independent/residual coding.
830
846
 * @note we assume enough bits are available, caller should check.
831
847
 * lsp10i() consumes 24 bits; lsp10r() consumes an additional 24 bits;
969
985
 
970
986
/**
971
987
 * @}
972
 
 * @defgroup aw Pitch-adaptive window coding functions
 
988
 * @name Pitch-adaptive window coding functions
973
989
 * The next few functions are for pitch-adaptive window coding.
974
990
 * @{
975
991
 */
1075
1091
            int excl_range         = s->aw_pulse_range; // always 16 or 24
1076
1092
            uint16_t *use_mask_ptr = &use_mask[idx >> 4];
1077
1093
            int first_sh           = 16 - (idx & 15);
1078
 
            *use_mask_ptr++       &= 0xFFFF << first_sh;
 
1094
            *use_mask_ptr++       &= 0xFFFFu << first_sh;
1079
1095
            excl_range            -= first_sh;
1080
1096
            if (excl_range >= 16) {
1081
1097
                *use_mask_ptr++    = 0;
1715
1731
 * @return 0 on success, <0 on error or 1 if there was not enough data to
1716
1732
 *         fully parse the superframe
1717
1733
 */
1718
 
static int synth_superframe(AVCodecContext *ctx,
1719
 
                            float *samples, int *data_size)
 
1734
static int synth_superframe(AVCodecContext *ctx, int *got_frame_ptr)
1720
1735
{
1721
1736
    WMAVoiceContext *s = ctx->priv_data;
1722
1737
    GetBitContext *gb = &s->gb, s_gb;
1726
1741
        wmavoice_mean_lsf16[s->lsp_def_mode] : wmavoice_mean_lsf10[s->lsp_def_mode];
1727
1742
    float excitation[MAX_SIGNAL_HISTORY + MAX_SFRAMESIZE + 12];
1728
1743
    float synth[MAX_LSPS + MAX_SFRAMESIZE];
 
1744
    float *samples;
1729
1745
 
1730
1746
    memcpy(synth,      s->synth_history,
1731
1747
           s->lsps             * sizeof(*synth));
1738
1754
        s->sframe_cache_size = 0;
1739
1755
    }
1740
1756
 
1741
 
    if ((res = check_bits_for_superframe(gb, s)) == 1) return 1;
 
1757
    if ((res = check_bits_for_superframe(gb, s)) == 1) {
 
1758
        *got_frame_ptr = 0;
 
1759
        return 1;
 
1760
    }
1742
1761
 
1743
1762
    /* First bit is speech/music bit, it differentiates between WMAVoice
1744
1763
     * speech samples (the actual codec) and WMAVoice music samples, which
1779
1798
            stabilize_lsps(lsps[n], s->lsps);
1780
1799
    }
1781
1800
 
1782
 
    /* Parse frames, optionally preceeded by per-frame (independent) LSPs. */
 
1801
    /* get output buffer */
 
1802
    s->frame.nb_samples = 480;
 
1803
    if ((res = ctx->get_buffer(ctx, &s->frame)) < 0) {
 
1804
        av_log(ctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
1805
        return res;
 
1806
    }
 
1807
    s->frame.nb_samples = n_samples;
 
1808
    samples = (float *)s->frame.data[0];
 
1809
 
 
1810
    /* Parse frames, optionally preceded by per-frame (independent) LSPs. */
1783
1811
    for (n = 0; n < 3; n++) {
1784
1812
        if (!s->has_residual_lsps) {
1785
1813
            int m;
1798
1826
                               &samples[n * MAX_FRAMESIZE],
1799
1827
                               lsps[n], n == 0 ? s->prev_lsps : lsps[n - 1],
1800
1828
                               &excitation[s->history_nsamples + n * MAX_FRAMESIZE],
1801
 
                               &synth[s->lsps + n * MAX_FRAMESIZE])))
 
1829
                               &synth[s->lsps + n * MAX_FRAMESIZE]))) {
 
1830
            *got_frame_ptr = 0;
1802
1831
            return res;
 
1832
        }
1803
1833
    }
1804
1834
 
1805
1835
    /* Statistics? FIXME - we don't check for length, a slight overrun
1810
1840
        skip_bits(gb, 10 * (res + 1));
1811
1841
    }
1812
1842
 
1813
 
    /* Specify nr. of output samples */
1814
 
    *data_size = n_samples * sizeof(float);
 
1843
    *got_frame_ptr = 1;
1815
1844
 
1816
1845
    /* Update history */
1817
1846
    memcpy(s->prev_lsps,           lsps[2],
1862
1891
 * @param size size of the source data, in bytes
1863
1892
 * @param gb bit I/O context specifying the current position in the source.
1864
1893
 *           data. This function might use this to align the bit position to
1865
 
 *           a whole-byte boundary before calling #ff_copy_bits() on aligned
 
1894
 *           a whole-byte boundary before calling #avpriv_copy_bits() on aligned
1866
1895
 *           source data
1867
1896
 * @param nbits the amount of bits to copy from source to target
1868
1897
 *
1878
1907
    rmn_bits = rmn_bytes = get_bits_left(gb);
1879
1908
    if (rmn_bits < nbits)
1880
1909
        return;
 
1910
    if (nbits > pb->size_in_bits - put_bits_count(pb))
 
1911
        return;
1881
1912
    rmn_bits &= 7; rmn_bytes >>= 3;
1882
1913
    if ((rmn_bits = FFMIN(rmn_bits, nbits)) > 0)
1883
1914
        put_bits(pb, rmn_bits, get_bits(gb, rmn_bits));
1884
 
    ff_copy_bits(pb, data + size - rmn_bytes,
 
1915
    avpriv_copy_bits(pb, data + size - rmn_bytes,
1885
1916
                 FFMIN(nbits - rmn_bits, rmn_bytes << 3));
1886
1917
}
1887
1918
 
1897
1928
 * For more information about frames, see #synth_superframe().
1898
1929
 */
1899
1930
static int wmavoice_decode_packet(AVCodecContext *ctx, void *data,
1900
 
                                  int *data_size, AVPacket *avpkt)
 
1931
                                  int *got_frame_ptr, AVPacket *avpkt)
1901
1932
{
1902
1933
    WMAVoiceContext *s = ctx->priv_data;
1903
1934
    GetBitContext *gb = &s->gb;
1904
1935
    int size, res, pos;
1905
1936
 
1906
 
    if (*data_size < 480 * sizeof(float)) {
1907
 
        av_log(ctx, AV_LOG_ERROR,
1908
 
               "Output buffer too small (%d given - %zu needed)\n",
1909
 
               *data_size, 480 * sizeof(float));
1910
 
        return -1;
1911
 
    }
1912
 
    *data_size = 0;
1913
 
 
1914
1937
    /* Packets are sometimes a multiple of ctx->block_align, with a packet
1915
1938
     * header at each ctx->block_align bytes. However, Libav's ASF demuxer
1916
1939
     * feeds us ASF packets, which may concatenate multiple "codec" packets
1917
1940
     * in a single "muxer" packet, so we artificially emulate that by
1918
1941
     * capping the packet size at ctx->block_align. */
1919
1942
    for (size = avpkt->size; size > ctx->block_align; size -= ctx->block_align);
1920
 
    if (!size)
 
1943
    if (!size) {
 
1944
        *got_frame_ptr = 0;
1921
1945
        return 0;
 
1946
    }
1922
1947
    init_get_bits(&s->gb, avpkt->data, size << 3);
1923
1948
 
1924
1949
    /* size == ctx->block_align is used to indicate whether we are dealing with
1937
1962
                copy_bits(&s->pb, avpkt->data, size, gb, s->spillover_nbits);
1938
1963
                flush_put_bits(&s->pb);
1939
1964
                s->sframe_cache_size += s->spillover_nbits;
1940
 
                if ((res = synth_superframe(ctx, data, data_size)) == 0 &&
1941
 
                    *data_size > 0) {
 
1965
                if ((res = synth_superframe(ctx, got_frame_ptr)) == 0 &&
 
1966
                    *got_frame_ptr) {
1942
1967
                    cnt += s->spillover_nbits;
1943
1968
                    s->skip_bits_next = cnt & 7;
 
1969
                    *(AVFrame *)data = s->frame;
1944
1970
                    return cnt >> 3;
1945
1971
                } else
1946
1972
                    skip_bits_long (gb, s->spillover_nbits - cnt +
1955
1981
    s->sframe_cache_size = 0;
1956
1982
    s->skip_bits_next = 0;
1957
1983
    pos = get_bits_left(gb);
1958
 
    if ((res = synth_superframe(ctx, data, data_size)) < 0) {
 
1984
    if ((res = synth_superframe(ctx, got_frame_ptr)) < 0) {
1959
1985
        return res;
1960
 
    } else if (*data_size > 0) {
 
1986
    } else if (*got_frame_ptr) {
1961
1987
        int cnt = get_bits_count(gb);
1962
1988
        s->skip_bits_next = cnt & 7;
 
1989
        *(AVFrame *)data = s->frame;
1963
1990
        return cnt >> 3;
1964
1991
    } else if ((s->sframe_cache_size = pos) > 0) {
1965
1992
        /* rewind bit reader to start of last (incomplete) superframe... */
2020
2047
}
2021
2048
 
2022
2049
AVCodec ff_wmavoice_decoder = {
2023
 
    "wmavoice",
2024
 
    AVMEDIA_TYPE_AUDIO,
2025
 
    CODEC_ID_WMAVOICE,
2026
 
    sizeof(WMAVoiceContext),
2027
 
    wmavoice_decode_init,
2028
 
    NULL,
2029
 
    wmavoice_decode_end,
2030
 
    wmavoice_decode_packet,
2031
 
    CODEC_CAP_SUBFRAMES,
 
2050
    .name           = "wmavoice",
 
2051
    .type           = AVMEDIA_TYPE_AUDIO,
 
2052
    .id             = CODEC_ID_WMAVOICE,
 
2053
    .priv_data_size = sizeof(WMAVoiceContext),
 
2054
    .init           = wmavoice_decode_init,
 
2055
    .close          = wmavoice_decode_end,
 
2056
    .decode         = wmavoice_decode_packet,
 
2057
    .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
2032
2058
    .flush     = wmavoice_flush,
2033
2059
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Voice"),
2034
2060
};