~ubuntu-branches/ubuntu/trusty/libav/trusty-proposed

« back to all changes in this revision

Viewing changes to libavcodec/libvo-aacenc.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2013-10-22 23:24:08 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: package-import@ubuntu.com-20131022232408-b8tvvn4pyzri9mi3
Tags: 6:9.10-1ubuntu1
* Build all -extra flavors from this source package, as libav got demoted
  from main to universe, cf LP: #1243235
* Simplify debian/rules to follow exactly the code that debian executes
* New upstream (LP: #1180288) fixes lots of security issues (LP: #1242802)
* Merge from unstable, remaining changes:
  - build-depend on libtiff5-dev rather than libtiff4-dev,
    avoids FTBFS caused by imlib
  - follow the regular debian codepaths

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <vo-aacenc/cmnMemory.h>
24
24
 
25
25
#include "avcodec.h"
 
26
#include "audio_frame_queue.h"
 
27
#include "internal.h"
26
28
#include "mpeg4audio.h"
27
29
 
 
30
#define FRAME_SIZE 1024
 
31
#define ENC_DELAY  1600
 
32
 
28
33
typedef struct AACContext {
29
34
    VO_AUDIO_CODECAPI codec_api;
30
35
    VO_HANDLE handle;
31
36
    VO_MEM_OPERATOR mem_operator;
32
37
    VO_CODEC_INIT_USERDATA user_data;
 
38
    VO_PBYTE end_buffer;
 
39
    AudioFrameQueue afq;
 
40
    int last_frame;
 
41
    int last_samples;
33
42
} AACContext;
34
43
 
 
44
 
 
45
static int aac_encode_close(AVCodecContext *avctx)
 
46
{
 
47
    AACContext *s = avctx->priv_data;
 
48
 
 
49
    s->codec_api.Uninit(s->handle);
 
50
#if FF_API_OLD_ENCODE_AUDIO
 
51
    av_freep(&avctx->coded_frame);
 
52
#endif
 
53
    av_freep(&avctx->extradata);
 
54
    ff_af_queue_close(&s->afq);
 
55
    av_freep(&s->end_buffer);
 
56
 
 
57
    return 0;
 
58
}
 
59
 
35
60
static av_cold int aac_encode_init(AVCodecContext *avctx)
36
61
{
37
62
    AACContext *s = avctx->priv_data;
38
63
    AACENC_PARAM params = { 0 };
39
 
    int index;
 
64
    int index, ret;
40
65
 
 
66
#if FF_API_OLD_ENCODE_AUDIO
41
67
    avctx->coded_frame = avcodec_alloc_frame();
42
 
    avctx->frame_size = 1024;
 
68
    if (!avctx->coded_frame)
 
69
        return AVERROR(ENOMEM);
 
70
#endif
 
71
    avctx->frame_size = FRAME_SIZE;
 
72
    avctx->delay      = ENC_DELAY;
 
73
    s->last_frame     = 2;
 
74
    ff_af_queue_init(avctx, &s->afq);
 
75
 
 
76
    s->end_buffer = av_mallocz(avctx->frame_size * avctx->channels * 2);
 
77
    if (!s->end_buffer) {
 
78
        ret = AVERROR(ENOMEM);
 
79
        goto error;
 
80
    }
43
81
 
44
82
    voGetAACEncAPI(&s->codec_api);
45
83
 
59
97
    if (s->codec_api.SetParam(s->handle, VO_PID_AAC_ENCPARAM, &params)
60
98
        != VO_ERR_NONE) {
61
99
        av_log(avctx, AV_LOG_ERROR, "Unable to set encoding parameters\n");
62
 
        return AVERROR(EINVAL);
 
100
        ret = AVERROR(EINVAL);
 
101
        goto error;
63
102
    }
64
103
 
65
104
    for (index = 0; index < 16; index++)
68
107
    if (index == 16) {
69
108
        av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n",
70
109
                                    avctx->sample_rate);
71
 
        return AVERROR(ENOSYS);
 
110
        ret = AVERROR(ENOSYS);
 
111
        goto error;
72
112
    }
73
113
    if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
74
114
        avctx->extradata_size = 2;
75
115
        avctx->extradata      = av_mallocz(avctx->extradata_size +
76
116
                                           FF_INPUT_BUFFER_PADDING_SIZE);
77
 
        if (!avctx->extradata)
78
 
            return AVERROR(ENOMEM);
 
117
        if (!avctx->extradata) {
 
118
            ret = AVERROR(ENOMEM);
 
119
            goto error;
 
120
        }
79
121
 
80
122
        avctx->extradata[0] = 0x02 << 3 | index >> 1;
81
123
        avctx->extradata[1] = (index & 0x01) << 7 | avctx->channels << 3;
82
124
    }
83
125
    return 0;
84
 
}
85
 
 
86
 
static int aac_encode_close(AVCodecContext *avctx)
87
 
{
88
 
    AACContext *s = avctx->priv_data;
89
 
 
90
 
    s->codec_api.Uninit(s->handle);
91
 
    av_freep(&avctx->coded_frame);
92
 
 
93
 
    return 0;
94
 
}
95
 
 
96
 
static int aac_encode_frame(AVCodecContext *avctx,
97
 
                            unsigned char *frame/*out*/,
98
 
                            int buf_size, void *data/*in*/)
 
126
error:
 
127
    aac_encode_close(avctx);
 
128
    return ret;
 
129
}
 
130
 
 
131
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
132
                            const AVFrame *frame, int *got_packet_ptr)
99
133
{
100
134
    AACContext *s = avctx->priv_data;
101
135
    VO_CODECBUFFER input = { 0 }, output = { 0 };
102
136
    VO_AUDIO_OUTPUTINFO output_info = { { 0 } };
103
 
 
104
 
    input.Buffer = data;
105
 
    input.Length = 2 * avctx->channels * avctx->frame_size;
106
 
    output.Buffer = frame;
107
 
    output.Length = buf_size;
 
137
    VO_PBYTE samples;
 
138
    int ret;
 
139
 
 
140
    /* handle end-of-stream small frame and flushing */
 
141
    if (!frame) {
 
142
        if (s->last_frame <= 0)
 
143
            return 0;
 
144
        if (s->last_samples > 0 && s->last_samples < ENC_DELAY - FRAME_SIZE) {
 
145
            s->last_samples = 0;
 
146
            s->last_frame--;
 
147
        }
 
148
        s->last_frame--;
 
149
        memset(s->end_buffer, 0, 2 * avctx->channels * avctx->frame_size);
 
150
        samples = s->end_buffer;
 
151
    } else {
 
152
        if (frame->nb_samples < avctx->frame_size) {
 
153
            s->last_samples = frame->nb_samples;
 
154
            memcpy(s->end_buffer, frame->data[0], 2 * avctx->channels * frame->nb_samples);
 
155
            samples = s->end_buffer;
 
156
        } else {
 
157
            samples = (VO_PBYTE)frame->data[0];
 
158
        }
 
159
        /* add current frame to the queue */
 
160
        if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
 
161
            return ret;
 
162
    }
 
163
 
 
164
    if ((ret = ff_alloc_packet(avpkt, FFMAX(8192, 768 * avctx->channels)))) {
 
165
        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
 
166
        return ret;
 
167
    }
 
168
 
 
169
    input.Buffer  = samples;
 
170
    input.Length  = 2 * avctx->channels * avctx->frame_size;
 
171
    output.Buffer = avpkt->data;
 
172
    output.Length = avpkt->size;
108
173
 
109
174
    s->codec_api.SetInputData(s->handle, &input);
110
175
    if (s->codec_api.GetOutputData(s->handle, &output, &output_info)
112
177
        av_log(avctx, AV_LOG_ERROR, "Unable to encode frame\n");
113
178
        return AVERROR(EINVAL);
114
179
    }
115
 
    return output.Length;
 
180
 
 
181
    /* Get the next frame pts/duration */
 
182
    ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
 
183
                       &avpkt->duration);
 
184
 
 
185
    avpkt->size = output.Length;
 
186
    *got_packet_ptr = 1;
 
187
    return 0;
116
188
}
117
189
 
118
190
AVCodec ff_libvo_aacenc_encoder = {
119
191
    .name           = "libvo_aacenc",
120
192
    .type           = AVMEDIA_TYPE_AUDIO,
121
 
    .id             = CODEC_ID_AAC,
 
193
    .id             = AV_CODEC_ID_AAC,
122
194
    .priv_data_size = sizeof(AACContext),
123
195
    .init           = aac_encode_init,
124
 
    .encode         = aac_encode_frame,
 
196
    .encode2        = aac_encode_frame,
125
197
    .close          = aac_encode_close,
126
 
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
127
 
    .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AAC"),
 
198
    .capabilities   = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
 
199
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
 
200
                                                     AV_SAMPLE_FMT_NONE },
 
201
    .long_name      = NULL_IF_CONFIG_SMALL("Android VisualOn AAC (Advanced Audio Coding)"),
128
202
};
129