]> git.sesse.net Git - ffmpeg/blob - libavcodec/roqaudioenc.c
dfa: convert to bytestream2 API
[ffmpeg] / libavcodec / roqaudioenc.c
1 /*
2  * RoQ audio encoder
3  *
4  * Copyright (c) 2005 Eric Lasota
5  *    Based on RoQ specs (c)2001 Tim Ferguson
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "libavutil/intmath.h"
25 #include "avcodec.h"
26 #include "bytestream.h"
27
28 #define ROQ_FRAME_SIZE           735
29 #define ROQ_HEADER_SIZE   8
30
31 #define MAX_DPCM (127*127)
32
33
34 typedef struct
35 {
36     short lastSample[2];
37     int input_frames;
38     int buffered_samples;
39     int16_t *frame_buffer;
40 } ROQDPCMContext;
41
42
43 static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
44 {
45     ROQDPCMContext *context = avctx->priv_data;
46
47     av_freep(&avctx->coded_frame);
48     av_freep(&context->frame_buffer);
49
50     return 0;
51 }
52
53 static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
54 {
55     ROQDPCMContext *context = avctx->priv_data;
56     int ret;
57
58     if (avctx->channels > 2) {
59         av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
60         return AVERROR(EINVAL);
61     }
62     if (avctx->sample_rate != 22050) {
63         av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
64         return AVERROR(EINVAL);
65     }
66
67     avctx->frame_size = ROQ_FRAME_SIZE;
68     avctx->bit_rate   = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
69                         (22050 / ROQ_FRAME_SIZE) * 8;
70
71     context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
72                                       sizeof(*context->frame_buffer));
73     if (!context->frame_buffer) {
74         ret = AVERROR(ENOMEM);
75         goto error;
76     }
77
78     context->lastSample[0] = context->lastSample[1] = 0;
79
80     avctx->coded_frame= avcodec_alloc_frame();
81     if (!avctx->coded_frame) {
82         ret = AVERROR(ENOMEM);
83         goto error;
84     }
85
86     return 0;
87 error:
88     roq_dpcm_encode_close(avctx);
89     return ret;
90 }
91
92 static unsigned char dpcm_predict(short *previous, short current)
93 {
94     int diff;
95     int negative;
96     int result;
97     int predicted;
98
99     diff = current - *previous;
100
101     negative = diff<0;
102     diff = FFABS(diff);
103
104     if (diff >= MAX_DPCM)
105         result = 127;
106     else {
107         result = ff_sqrt(diff);
108         result += diff > result*result+result;
109     }
110
111     /* See if this overflows */
112  retry:
113     diff = result*result;
114     if (negative)
115         diff = -diff;
116     predicted = *previous + diff;
117
118     /* If it overflows, back off a step */
119     if (predicted > 32767 || predicted < -32768) {
120         result--;
121         goto retry;
122     }
123
124     /* Add the sign bit */
125     result |= negative << 7;   //if (negative) result |= 128;
126
127     *previous = predicted;
128
129     return result;
130 }
131
132 static int roq_dpcm_encode_frame(AVCodecContext *avctx,
133                 unsigned char *frame, int buf_size, void *data)
134 {
135     int i, stereo, data_size;
136     const int16_t *in = data;
137     uint8_t *out = frame;
138     ROQDPCMContext *context = avctx->priv_data;
139
140     stereo = (avctx->channels == 2);
141
142     if (!data && context->input_frames >= 8)
143         return 0;
144
145     if (data && context->input_frames < 8) {
146         memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
147                in, avctx->frame_size * avctx->channels * sizeof(*in));
148         context->buffered_samples += avctx->frame_size;
149         if (context->input_frames < 7) {
150             context->input_frames++;
151             return 0;
152         }
153         in = context->frame_buffer;
154     }
155
156     if (stereo) {
157         context->lastSample[0] &= 0xFF00;
158         context->lastSample[1] &= 0xFF00;
159     }
160
161     if (context->input_frames == 7 || !data)
162         data_size = avctx->channels * context->buffered_samples;
163     else
164         data_size = avctx->channels * avctx->frame_size;
165
166     if (buf_size < ROQ_HEADER_SIZE + data_size) {
167         av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
168         return AVERROR(EINVAL);
169     }
170
171     bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
172     bytestream_put_byte(&out, 0x10);
173     bytestream_put_le32(&out, data_size);
174
175     if (stereo) {
176         bytestream_put_byte(&out, (context->lastSample[1])>>8);
177         bytestream_put_byte(&out, (context->lastSample[0])>>8);
178     } else
179         bytestream_put_le16(&out, context->lastSample[0]);
180
181     /* Write the actual samples */
182     for (i = 0; i < data_size; i++)
183         *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
184
185     context->input_frames++;
186     if (!data)
187         context->input_frames = FFMAX(context->input_frames, 8);
188
189     /* Return the result size */
190     return ROQ_HEADER_SIZE + data_size;
191 }
192
193 AVCodec ff_roq_dpcm_encoder = {
194     .name           = "roq_dpcm",
195     .type           = AVMEDIA_TYPE_AUDIO,
196     .id             = CODEC_ID_ROQ_DPCM,
197     .priv_data_size = sizeof(ROQDPCMContext),
198     .init           = roq_dpcm_encode_init,
199     .encode         = roq_dpcm_encode_frame,
200     .close          = roq_dpcm_encode_close,
201     .capabilities   = CODEC_CAP_DELAY,
202     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
203     .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
204 };