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