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

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/mpegaudioenc.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:
24
24
 * The simplest mpeg audio layer 2 encoder.
25
25
 */
26
26
 
 
27
#include "libavutil/channel_layout.h"
 
28
 
27
29
#include "avcodec.h"
28
30
#include "internal.h"
29
31
#include "put_bits.h"
32
34
#define WFRAC_BITS  14   /* fractional bits for window */
33
35
 
34
36
#include "mpegaudio.h"
 
37
#include "mpegaudiodsp.h"
35
38
 
36
39
/* currently, cannot change these constants (need to modify
37
40
   quantization stage) */
75
78
 
76
79
    if (channels <= 0 || channels > 2){
77
80
        av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
78
 
        return -1;
 
81
        return AVERROR(EINVAL);
79
82
    }
80
83
    bitrate = bitrate / 1000;
81
84
    s->nb_channels = channels;
82
85
    avctx->frame_size = MPA_FRAME_SIZE;
 
86
    avctx->delay      = 512 - 32 + 1;
83
87
 
84
88
    /* encoding freq */
85
89
    s->lsf = 0;
93
97
    }
94
98
    if (i == 3){
95
99
        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
96
 
        return -1;
 
100
        return AVERROR(EINVAL);
97
101
    }
98
102
    s->freq_index = i;
99
103
 
104
108
    }
105
109
    if (i == 15){
106
110
        av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
107
 
        return -1;
 
111
        return AVERROR(EINVAL);
108
112
    }
109
113
    s->bitrate_index = i;
110
114
 
180
184
        total_quant_bits[i] = 12 * v;
181
185
    }
182
186
 
 
187
#if FF_API_OLD_ENCODE_AUDIO
183
188
    avctx->coded_frame= avcodec_alloc_frame();
184
 
    avctx->coded_frame->key_frame= 1;
 
189
    if (!avctx->coded_frame)
 
190
        return AVERROR(ENOMEM);
 
191
#endif
185
192
 
186
193
    return 0;
187
194
}
725
732
    flush_put_bits(p);
726
733
}
727
734
 
728
 
static int MPA_encode_frame(AVCodecContext *avctx,
729
 
                            unsigned char *frame, int buf_size, void *data)
 
735
static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
736
                            const AVFrame *frame, int *got_packet_ptr)
730
737
{
731
738
    MpegAudioContext *s = avctx->priv_data;
732
 
    const short *samples = data;
 
739
    const int16_t *samples = (const int16_t *)frame->data[0];
733
740
    short smr[MPA_MAX_CHANNELS][SBLIMIT];
734
741
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
735
 
    int padding, i;
 
742
    int padding, i, ret;
736
743
 
737
744
    for(i=0;i<s->nb_channels;i++) {
738
745
        filter(s, i, samples + i, s->nb_channels);
747
754
    }
748
755
    compute_bit_allocation(s, smr, bit_alloc, &padding);
749
756
 
750
 
    init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
 
757
    if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE))) {
 
758
        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
 
759
        return ret;
 
760
    }
 
761
 
 
762
    init_put_bits(&s->pb, avpkt->data, avpkt->size);
751
763
 
752
764
    encode_frame(s, bit_alloc, padding);
753
765
 
754
 
    return put_bits_ptr(&s->pb) - s->pb.buf;
 
766
    if (frame->pts != AV_NOPTS_VALUE)
 
767
        avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
 
768
 
 
769
    avpkt->size = put_bits_count(&s->pb) / 8;
 
770
    *got_packet_ptr = 1;
 
771
    return 0;
755
772
}
756
773
 
757
774
static av_cold int MPA_encode_close(AVCodecContext *avctx)
758
775
{
 
776
#if FF_API_OLD_ENCODE_AUDIO
759
777
    av_freep(&avctx->coded_frame);
 
778
#endif
760
779
    return 0;
761
780
}
762
781
 
766
785
};
767
786
 
768
787
AVCodec ff_mp2_encoder = {
769
 
    .name           = "mp2",
770
 
    .type           = AVMEDIA_TYPE_AUDIO,
771
 
    .id             = CODEC_ID_MP2,
772
 
    .priv_data_size = sizeof(MpegAudioContext),
773
 
    .init           = MPA_encode_init,
774
 
    .encode         = MPA_encode_frame,
775
 
    .close          = MPA_encode_close,
776
 
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
777
 
    .supported_samplerates= (const int[]){44100, 48000,  32000, 22050, 24000, 16000, 0},
778
 
    .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
779
 
    .defaults       = mp2_defaults,
 
788
    .name                  = "mp2",
 
789
    .type                  = AVMEDIA_TYPE_AUDIO,
 
790
    .id                    = AV_CODEC_ID_MP2,
 
791
    .priv_data_size        = sizeof(MpegAudioContext),
 
792
    .init                  = MPA_encode_init,
 
793
    .encode2               = MPA_encode_frame,
 
794
    .close                 = MPA_encode_close,
 
795
    .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
 
796
                                                            AV_SAMPLE_FMT_NONE },
 
797
    .supported_samplerates = (const int[]){
 
798
        44100, 48000,  32000, 22050, 24000, 16000, 0
 
799
    },
 
800
    .channel_layouts       = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
 
801
                                                 AV_CH_LAYOUT_STEREO,
 
802
                                                 0 },
 
803
    .long_name             = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
 
804
    .defaults              = mp2_defaults,
780
805
};