]> git.sesse.net Git - ffmpeg/blob - libavcodec/roqaudioenc.c
zmbv: remove unused variable
[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 FFmpeg.
8  *
9  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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_FIRST_FRAME_SIZE     (735*8)
29 #define ROQ_FRAME_SIZE           735
30
31
32 #define MAX_DPCM (127*127)
33
34
35 typedef struct
36 {
37     short lastSample[2];
38 } ROQDPCMContext;
39
40 static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
41 {
42     ROQDPCMContext *context = avctx->priv_data;
43
44     if (avctx->channels > 2) {
45         av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
46         return -1;
47     }
48     if (avctx->sample_rate != 22050) {
49         av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
50         return -1;
51     }
52     if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
53         av_log(avctx, AV_LOG_ERROR, "Audio must be signed 16-bit\n");
54         return -1;
55     }
56
57     avctx->frame_size = ROQ_FIRST_FRAME_SIZE;
58
59     context->lastSample[0] = context->lastSample[1] = 0;
60
61     avctx->coded_frame= avcodec_alloc_frame();
62     if (!avctx->coded_frame)
63         return AVERROR(ENOMEM);
64
65     return 0;
66 }
67
68 static unsigned char dpcm_predict(short *previous, short current)
69 {
70     int diff;
71     int negative;
72     int result;
73     int predicted;
74
75     diff = current - *previous;
76
77     negative = diff<0;
78     diff = FFABS(diff);
79
80     if (diff >= MAX_DPCM)
81         result = 127;
82     else {
83         result = ff_sqrt(diff);
84         result += diff > result*result+result;
85     }
86
87     /* See if this overflows */
88  retry:
89     diff = result*result;
90     if (negative)
91         diff = -diff;
92     predicted = *previous + diff;
93
94     /* If it overflows, back off a step */
95     if (predicted > 32767 || predicted < -32768) {
96         result--;
97         goto retry;
98     }
99
100     /* Add the sign bit */
101     result |= negative << 7;   //if (negative) result |= 128;
102
103     *previous = predicted;
104
105     return result;
106 }
107
108 static int roq_dpcm_encode_frame(AVCodecContext *avctx,
109                 unsigned char *frame, int buf_size, void *data)
110 {
111     int i, samples, stereo, ch;
112     const short *in;
113     unsigned char *out;
114
115     ROQDPCMContext *context = avctx->priv_data;
116
117     stereo = (avctx->channels == 2);
118
119     if (stereo) {
120         context->lastSample[0] &= 0xFF00;
121         context->lastSample[1] &= 0xFF00;
122     }
123
124     out = frame;
125     in = data;
126
127     bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
128     bytestream_put_byte(&out, 0x10);
129     bytestream_put_le32(&out, avctx->frame_size*avctx->channels);
130
131     if (stereo) {
132         bytestream_put_byte(&out, (context->lastSample[1])>>8);
133         bytestream_put_byte(&out, (context->lastSample[0])>>8);
134     } else
135         bytestream_put_le16(&out, context->lastSample[0]);
136
137     /* Write the actual samples */
138     samples = avctx->frame_size;
139     for (i=0; i<samples; i++)
140         for (ch=0; ch<avctx->channels; ch++)
141             *out++ = dpcm_predict(&context->lastSample[ch], *in++);
142
143     /* Use smaller frames from now on */
144     avctx->frame_size = ROQ_FRAME_SIZE;
145
146     /* Return the result size */
147     return out - frame;
148 }
149
150 static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
151 {
152     av_freep(&avctx->coded_frame);
153
154     return 0;
155 }
156
157 AVCodec ff_roq_dpcm_encoder = {
158     .name           = "roq_dpcm",
159     .type           = AVMEDIA_TYPE_AUDIO,
160     .id             = CODEC_ID_ROQ_DPCM,
161     .priv_data_size = sizeof(ROQDPCMContext),
162     .init           = roq_dpcm_encode_init,
163     .encode         = roq_dpcm_encode_frame,
164     .close          = roq_dpcm_encode_close,
165     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
166     .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
167 };