~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/atrac3.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:
41
41
#include "dsputil.h"
42
42
#include "bytestream.h"
43
43
#include "fft.h"
 
44
#include "fmtconvert.h"
44
45
 
45
46
#include "atrac.h"
46
47
#include "atrac3data.h"
48
49
#define JOINT_STEREO    0x12
49
50
#define STEREO          0x2
50
51
 
 
52
#define SAMPLES_PER_FRAME 1024
 
53
#define MDCT_SIZE          512
51
54
 
52
55
/* These structures are needed to store the parsed gain control data. */
53
56
typedef struct {
70
73
    int               bandsCoded;
71
74
    int               numComponents;
72
75
    tonal_component   components[64];
73
 
    float             prevFrame[1024];
 
76
    float             prevFrame[SAMPLES_PER_FRAME];
74
77
    int               gcBlkSwitch;
75
78
    gain_block        gainBlock[2];
76
79
 
77
 
    DECLARE_ALIGNED(32, float, spectrum)[1024];
78
 
    DECLARE_ALIGNED(32, float, IMDCT_buf)[1024];
 
80
    DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
 
81
    DECLARE_ALIGNED(32, float, IMDCT_buf)[SAMPLES_PER_FRAME];
79
82
 
80
83
    float             delayBuf1[46]; ///<qmf delay buffers
81
84
    float             delayBuf2[46];
83
86
} channel_unit;
84
87
 
85
88
typedef struct {
 
89
    AVFrame             frame;
86
90
    GetBitContext       gb;
87
91
    //@{
88
92
    /** stream data */
107
111
    //@}
108
112
    //@{
109
113
    /** data buffers */
110
 
    float               outSamples[2048];
 
114
    float              *outSamples[2];
111
115
    uint8_t*            decoded_bytes_buffer;
112
116
    float               tempBuf[1070];
113
117
    //@}
120
124
    //@}
121
125
 
122
126
    FFTContext          mdct_ctx;
 
127
    FmtConvertContext   fmt_conv;
123
128
} ATRAC3Context;
124
129
 
125
 
static DECLARE_ALIGNED(32, float, mdct_window)[512];
 
130
static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
126
131
static VLC              spectral_coeff_tab[7];
127
132
static float            gain_tab1[16];
128
133
static float            gain_tab2[31];
159
164
    q->mdct_ctx.imdct_calc(&q->mdct_ctx,pOutput,pInput);
160
165
 
161
166
    /* Perform windowing on the output. */
162
 
    dsp.vector_fmul(pOutput, pOutput, mdct_window, 512);
 
167
    dsp.vector_fmul(pOutput, pOutput, mdct_window, MDCT_SIZE);
163
168
 
164
169
}
165
170
 
192
197
}
193
198
 
194
199
 
195
 
static av_cold void init_atrac3_transforms(ATRAC3Context *q) {
 
200
static av_cold int init_atrac3_transforms(ATRAC3Context *q, int is_float) {
196
201
    float enc_window[256];
197
202
    int i;
198
203
 
208
213
        }
209
214
 
210
215
    /* Initialize the MDCT transform. */
211
 
    ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0);
 
216
    return ff_mdct_init(&q->mdct_ctx, 9, 1, is_float ? 1.0 / 32768 : 1.0);
212
217
}
213
218
 
214
219
/**
221
226
 
222
227
    av_free(q->pUnits);
223
228
    av_free(q->decoded_bytes_buffer);
 
229
    av_freep(&q->outSamples[0]);
 
230
 
224
231
    ff_mdct_end(&q->mdct_ctx);
225
232
 
226
233
    return 0;
340
347
 
341
348
    /* Clear the subbands that were not coded. */
342
349
    first = subbandTab[cnt];
343
 
    memset(pOut+first, 0, (1024 - first) * sizeof(float));
 
350
    memset(pOut+first, 0, (SAMPLES_PER_FRAME - first) * sizeof(float));
344
351
    return numSubbands;
345
352
}
346
353
 
370
377
 
371
378
    coding_mode_selector = get_bits(gb,2);
372
379
    if (coding_mode_selector == 2)
373
 
        return -1;
 
380
        return AVERROR_INVALIDDATA;
374
381
 
375
382
    coding_mode = coding_mode_selector & 1;
376
383
 
382
389
 
383
390
        quant_step_index = get_bits(gb,3);
384
391
        if (quant_step_index <= 1)
385
 
            return -1;
 
392
            return AVERROR_INVALIDDATA;
386
393
 
387
394
        if (coding_mode_selector == 3)
388
395
            coding_mode = get_bits1(gb);
396
403
            for (k=0; k<coded_components; k++) {
397
404
                sfIndx = get_bits(gb,6);
398
405
                pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
399
 
                max_coded_values = 1024 - pComponent[component_count].pos;
 
406
                max_coded_values = SAMPLES_PER_FRAME - pComponent[component_count].pos;
400
407
                coded_values = coded_values_per_component + 1;
401
408
                coded_values = FFMIN(max_coded_values,coded_values);
402
409
 
445
452
            pLevel[cf]= get_bits(gb,4);
446
453
            pLoc  [cf]= get_bits(gb,5);
447
454
            if(cf && pLoc[cf] <= pLoc[cf-1])
448
 
                return -1;
 
455
                return AVERROR_INVALIDDATA;
449
456
        }
450
457
    }
451
458
 
662
669
    if (codingMode == JOINT_STEREO && channelNum == 1) {
663
670
        if (get_bits(gb,2) != 3) {
664
671
            av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
665
 
            return -1;
 
672
            return AVERROR_INVALIDDATA;
666
673
        }
667
674
    } else {
668
675
        if (get_bits(gb,6) != 0x28) {
669
676
            av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
670
 
            return -1;
 
677
            return AVERROR_INVALIDDATA;
671
678
        }
672
679
    }
673
680
 
701
708
            memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
702
709
 
703
710
        /* gain compensation and overlapping */
704
 
        gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
705
 
                                    &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
706
 
                                    &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
 
711
        gainCompensateAndOverlap(pSnd->IMDCT_buf, &pSnd->prevFrame[band * 256],
 
712
                                 &pOut[band * 256],
 
713
                                 &pSnd->gainBlock[1 - pSnd->gcBlkSwitch].gBlock[band],
 
714
                                 &pSnd->gainBlock[    pSnd->gcBlkSwitch].gBlock[band]);
707
715
    }
708
716
 
709
717
    /* Swap the gain control buffers for the next frame. */
719
727
 * @param databuf       the input data
720
728
 */
721
729
 
722
 
static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
 
730
static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf,
 
731
                       float **out_samples)
723
732
{
724
733
    int   result, i;
725
734
    float   *p1, *p2, *p3, *p4;
731
740
        /* decode Sound Unit 1 */
732
741
        init_get_bits(&q->gb,databuf,q->bits_per_frame);
733
742
 
734
 
        result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
 
743
        result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, out_samples[0], 0, JOINT_STEREO);
735
744
        if (result != 0)
736
 
            return (result);
 
745
            return result;
737
746
 
738
747
        /* Framedata of the su2 in the joint-stereo mode is encoded in
739
748
         * reverse byte order so we need to swap it first. */
753
762
        ptr1 = q->decoded_bytes_buffer;
754
763
        for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
755
764
            if (i >= q->bytes_per_frame)
756
 
                return -1;
 
765
                return AVERROR_INVALIDDATA;
757
766
        }
758
767
 
759
768
 
772
781
        }
773
782
 
774
783
        /* Decode Sound Unit 2. */
775
 
        result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
 
784
        result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], out_samples[1], 1, JOINT_STEREO);
776
785
        if (result != 0)
777
 
            return (result);
 
786
            return result;
778
787
 
779
788
        /* Reconstruct the channel coefficients. */
780
 
        reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
 
789
        reverseMatrixing(out_samples[0], out_samples[1], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
781
790
 
782
 
        channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
 
791
        channelWeighting(out_samples[0], out_samples[1], q->weighting_delay);
783
792
 
784
793
    } else {
785
794
        /* normal stereo mode or mono */
787
796
        for (i=0 ; i<q->channels ; i++) {
788
797
 
789
798
            /* Set the bitstream reader at the start of a channel sound unit. */
790
 
            init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
 
799
            init_get_bits(&q->gb,
 
800
                          databuf + i * q->bytes_per_frame / q->channels,
 
801
                          q->bits_per_frame / q->channels);
791
802
 
792
 
            result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
 
803
            result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], out_samples[i], i, q->codingMode);
793
804
            if (result != 0)
794
 
                return (result);
 
805
                return result;
795
806
        }
796
807
    }
797
808
 
798
809
    /* Apply the iQMF synthesis filter. */
799
 
    p1= q->outSamples;
800
810
    for (i=0 ; i<q->channels ; i++) {
 
811
        p1 = out_samples[i];
801
812
        p2= p1+256;
802
813
        p3= p2+256;
803
814
        p4= p3+256;
804
815
        atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
805
816
        atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
806
817
        atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
807
 
        p1 +=1024;
808
818
    }
809
819
 
810
820
    return 0;
817
827
 * @param avctx     pointer to the AVCodecContext
818
828
 */
819
829
 
820
 
static int atrac3_decode_frame(AVCodecContext *avctx,
821
 
            void *data, int *data_size,
822
 
            AVPacket *avpkt) {
 
830
static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
 
831
                               int *got_frame_ptr, AVPacket *avpkt)
 
832
{
823
833
    const uint8_t *buf = avpkt->data;
824
834
    int buf_size = avpkt->size;
825
835
    ATRAC3Context *q = avctx->priv_data;
826
 
    int result = 0, i;
 
836
    int result;
827
837
    const uint8_t* databuf;
828
 
    int16_t* samples = data;
 
838
    float   *samples_flt;
 
839
    int16_t *samples_s16;
829
840
 
830
841
    if (buf_size < avctx->block_align) {
831
842
        av_log(avctx, AV_LOG_ERROR,
832
843
               "Frame too small (%d bytes). Truncated file?\n", buf_size);
833
 
        *data_size = 0;
834
 
        return buf_size;
835
 
    }
 
844
        return AVERROR_INVALIDDATA;
 
845
    }
 
846
 
 
847
    /* get output buffer */
 
848
    q->frame.nb_samples = SAMPLES_PER_FRAME;
 
849
    if ((result = avctx->get_buffer(avctx, &q->frame)) < 0) {
 
850
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
851
        return result;
 
852
    }
 
853
    samples_flt = (float   *)q->frame.data[0];
 
854
    samples_s16 = (int16_t *)q->frame.data[0];
836
855
 
837
856
    /* Check if we need to descramble and what buffer to pass on. */
838
857
    if (q->scrambled_stream) {
842
861
        databuf = buf;
843
862
    }
844
863
 
845
 
    result = decodeFrame(q, databuf);
 
864
    if (q->channels == 1 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
 
865
        result = decodeFrame(q, databuf, &samples_flt);
 
866
    else
 
867
        result = decodeFrame(q, databuf, q->outSamples);
846
868
 
847
869
    if (result != 0) {
848
870
        av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
849
 
        return -1;
850
 
    }
851
 
 
852
 
    if (q->channels == 1) {
853
 
        /* mono */
854
 
        for (i = 0; i<1024; i++)
855
 
            samples[i] = av_clip_int16(round(q->outSamples[i]));
856
 
        *data_size = 1024 * sizeof(int16_t);
857
 
    } else {
858
 
        /* stereo */
859
 
        for (i = 0; i < 1024; i++) {
860
 
            samples[i*2] = av_clip_int16(round(q->outSamples[i]));
861
 
            samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
862
 
        }
863
 
        *data_size = 2048 * sizeof(int16_t);
864
 
    }
 
871
        return result;
 
872
    }
 
873
 
 
874
    /* interleave */
 
875
    if (q->channels == 2 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
 
876
        q->fmt_conv.float_interleave(samples_flt,
 
877
                                     (const float **)q->outSamples,
 
878
                                     SAMPLES_PER_FRAME, 2);
 
879
    } else if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
 
880
        q->fmt_conv.float_to_int16_interleave(samples_s16,
 
881
                                              (const float **)q->outSamples,
 
882
                                              SAMPLES_PER_FRAME, q->channels);
 
883
    }
 
884
 
 
885
    *got_frame_ptr   = 1;
 
886
    *(AVFrame *)data = q->frame;
865
887
 
866
888
    return avctx->block_align;
867
889
}
875
897
 
876
898
static av_cold int atrac3_decode_init(AVCodecContext *avctx)
877
899
{
878
 
    int i;
 
900
    int i, ret;
879
901
    const uint8_t *edata_ptr = avctx->extradata;
880
902
    ATRAC3Context *q = avctx->priv_data;
881
903
    static VLC_TYPE atrac3_vlc_table[4096][2];
899
921
        av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));  //Unknown always 0
900
922
 
901
923
        /* setup */
902
 
        q->samples_per_frame = 1024 * q->channels;
 
924
        q->samples_per_frame = SAMPLES_PER_FRAME * q->channels;
903
925
        q->atrac3version = 4;
904
926
        q->delay = 0x88E;
905
927
        if (q->codingMode)
912
934
        if ((q->bytes_per_frame == 96*q->channels*q->frame_factor) || (q->bytes_per_frame == 152*q->channels*q->frame_factor) || (q->bytes_per_frame == 192*q->channels*q->frame_factor)) {
913
935
        } else {
914
936
            av_log(avctx,AV_LOG_ERROR,"Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q->bytes_per_frame, q->channels, q->frame_factor);
915
 
            return -1;
 
937
            return AVERROR_INVALIDDATA;
916
938
        }
917
939
 
918
940
    } else if (avctx->extradata_size == 10) {
932
954
 
933
955
    if (q->atrac3version != 4) {
934
956
        av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
935
 
        return -1;
 
957
        return AVERROR_INVALIDDATA;
936
958
    }
937
959
 
938
 
    if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
 
960
    if (q->samples_per_frame != SAMPLES_PER_FRAME && q->samples_per_frame != SAMPLES_PER_FRAME*2) {
939
961
        av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
940
 
        return -1;
 
962
        return AVERROR_INVALIDDATA;
941
963
    }
942
964
 
943
965
    if (q->delay != 0x88E) {
944
966
        av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
945
 
        return -1;
 
967
        return AVERROR_INVALIDDATA;
946
968
    }
947
969
 
948
970
    if (q->codingMode == STEREO) {
951
973
        av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
952
974
    } else {
953
975
        av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
954
 
        return -1;
 
976
        return AVERROR_INVALIDDATA;
955
977
    }
956
978
 
957
979
    if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) {
958
980
        av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
959
 
        return -1;
 
981
        return AVERROR(EINVAL);
960
982
    }
961
983
 
962
984
 
963
985
    if(avctx->block_align >= UINT_MAX/2)
964
 
        return -1;
 
986
        return AVERROR(EINVAL);
965
987
 
966
988
    /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
967
989
     * this is for the bitstream reader. */
981
1003
        vlcs_initialized = 1;
982
1004
    }
983
1005
 
984
 
    init_atrac3_transforms(q);
 
1006
    if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT)
 
1007
        avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
 
1008
    else
 
1009
        avctx->sample_fmt = AV_SAMPLE_FMT_S16;
 
1010
 
 
1011
    if ((ret = init_atrac3_transforms(q, avctx->sample_fmt == AV_SAMPLE_FMT_FLT))) {
 
1012
        av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
 
1013
        av_freep(&q->decoded_bytes_buffer);
 
1014
        return ret;
 
1015
    }
985
1016
 
986
1017
    atrac_generate_tables();
987
1018
 
1007
1038
    }
1008
1039
 
1009
1040
    dsputil_init(&dsp, avctx);
 
1041
    ff_fmt_convert_init(&q->fmt_conv, avctx);
1010
1042
 
1011
1043
    q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
1012
1044
    if (!q->pUnits) {
1013
 
        av_free(q->decoded_bytes_buffer);
 
1045
        atrac3_decode_close(avctx);
1014
1046
        return AVERROR(ENOMEM);
1015
1047
    }
1016
1048
 
1017
 
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
 
1049
    if (avctx->channels > 1 || avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
 
1050
        q->outSamples[0] = av_mallocz(SAMPLES_PER_FRAME * avctx->channels * sizeof(*q->outSamples[0]));
 
1051
        q->outSamples[1] = q->outSamples[0] + SAMPLES_PER_FRAME;
 
1052
        if (!q->outSamples[0]) {
 
1053
            atrac3_decode_close(avctx);
 
1054
            return AVERROR(ENOMEM);
 
1055
        }
 
1056
    }
 
1057
 
 
1058
    avcodec_get_frame_defaults(&q->frame);
 
1059
    avctx->coded_frame = &q->frame;
 
1060
 
1018
1061
    return 0;
1019
1062
}
1020
1063
 
1028
1071
    .init = atrac3_decode_init,
1029
1072
    .close = atrac3_decode_close,
1030
1073
    .decode = atrac3_decode_frame,
 
1074
    .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1031
1075
    .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
1032
1076
};