OSDN Git Service

avcodec/vp9block: fix runtime error: signed integer overflow: 196675 * 20670 cannot...
[android-x86/external-ffmpeg.git] / libavcodec / cngdec.c
1 /*
2  * RFC 3389 comfort noise generator
3  * Copyright (c) 2012 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <math.h>
23
24 #include "libavutil/common.h"
25 #include "libavutil/ffmath.h"
26 #include "avcodec.h"
27 #include "celp_filters.h"
28 #include "internal.h"
29 #include "libavutil/lfg.h"
30
31 typedef struct CNGContext {
32     float *refl_coef, *target_refl_coef;
33     float *lpc_coef;
34     int order;
35     int energy, target_energy;
36     int inited;
37     float *filter_out;
38     float *excitation;
39     AVLFG lfg;
40 } CNGContext;
41
42 static av_cold int cng_decode_close(AVCodecContext *avctx)
43 {
44     CNGContext *p = avctx->priv_data;
45     av_freep(&p->refl_coef);
46     av_freep(&p->target_refl_coef);
47     av_freep(&p->lpc_coef);
48     av_freep(&p->filter_out);
49     av_freep(&p->excitation);
50     return 0;
51 }
52
53 static av_cold int cng_decode_init(AVCodecContext *avctx)
54 {
55     CNGContext *p = avctx->priv_data;
56
57     avctx->sample_fmt  = AV_SAMPLE_FMT_S16;
58     avctx->channels    = 1;
59     avctx->sample_rate = 8000;
60
61     p->order            = 12;
62     avctx->frame_size   = 640;
63     p->refl_coef        = av_mallocz_array(p->order, sizeof(*p->refl_coef));
64     p->target_refl_coef = av_mallocz_array(p->order, sizeof(*p->target_refl_coef));
65     p->lpc_coef         = av_mallocz_array(p->order, sizeof(*p->lpc_coef));
66     p->filter_out       = av_mallocz_array(avctx->frame_size + p->order,
67                                      sizeof(*p->filter_out));
68     p->excitation       = av_mallocz_array(avctx->frame_size, sizeof(*p->excitation));
69     if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
70         !p->filter_out || !p->excitation) {
71         cng_decode_close(avctx);
72         return AVERROR(ENOMEM);
73     }
74
75     av_lfg_init(&p->lfg, 0);
76
77     return 0;
78 }
79
80 static void make_lpc_coefs(float *lpc, const float *refl, int order)
81 {
82     float buf[100];
83     float *next, *cur;
84     int m, i;
85     next = buf;
86     cur  = lpc;
87     for (m = 0; m < order; m++) {
88         next[m] = refl[m];
89         for (i = 0; i < m; i++)
90             next[i] = cur[i] + refl[m] * cur[m - i - 1];
91         FFSWAP(float*, next, cur);
92     }
93     if (cur != lpc)
94         memcpy(lpc, cur, sizeof(*lpc) * order);
95 }
96
97 static void cng_decode_flush(AVCodecContext *avctx)
98 {
99     CNGContext *p = avctx->priv_data;
100     p->inited = 0;
101 }
102
103 static int cng_decode_frame(AVCodecContext *avctx, void *data,
104                             int *got_frame_ptr, AVPacket *avpkt)
105 {
106     AVFrame *frame = data;
107     CNGContext *p = avctx->priv_data;
108     int buf_size  = avpkt->size;
109     int ret, i;
110     int16_t *buf_out;
111     float e = 1.0;
112     float scaling;
113
114     if (avpkt->size) {
115         int dbov = -avpkt->data[0];
116         p->target_energy = 1081109975 * ff_exp10(dbov / 10.0) * 0.75;
117         memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
118         for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
119             p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
120         }
121     }
122
123     if (p->inited) {
124         p->energy = p->energy / 2 + p->target_energy / 2;
125         for (i = 0; i < p->order; i++)
126             p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
127     } else {
128         p->energy = p->target_energy;
129         memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
130         p->inited = 1;
131     }
132     make_lpc_coefs(p->lpc_coef, p->refl_coef, p->order);
133
134     for (i = 0; i < p->order; i++)
135         e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
136
137     scaling = sqrt(e * p->energy / 1081109975);
138     for (i = 0; i < avctx->frame_size; i++) {
139         int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
140         p->excitation[i] = scaling * r;
141     }
142     ff_celp_lp_synthesis_filterf(p->filter_out + p->order, p->lpc_coef,
143                                  p->excitation, avctx->frame_size, p->order);
144
145     frame->nb_samples = avctx->frame_size;
146     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
147         return ret;
148     buf_out = (int16_t *)frame->data[0];
149     for (i = 0; i < avctx->frame_size; i++)
150         buf_out[i] = p->filter_out[i + p->order];
151     memcpy(p->filter_out, p->filter_out + avctx->frame_size,
152            p->order * sizeof(*p->filter_out));
153
154     *got_frame_ptr = 1;
155
156     return buf_size;
157 }
158
159 AVCodec ff_comfortnoise_decoder = {
160     .name           = "comfortnoise",
161     .long_name      = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
162     .type           = AVMEDIA_TYPE_AUDIO,
163     .id             = AV_CODEC_ID_COMFORT_NOISE,
164     .priv_data_size = sizeof(CNGContext),
165     .init           = cng_decode_init,
166     .decode         = cng_decode_frame,
167     .flush          = cng_decode_flush,
168     .close          = cng_decode_close,
169     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
170                                                      AV_SAMPLE_FMT_NONE },
171     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
172 };