]> git.sesse.net Git - ffmpeg/blob - libavcodec/roqaudioenc.c
avcodec: Constify AVCodecs
[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 "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "mathops.h"
28
29 #define ROQ_FRAME_SIZE           735
30 #define ROQ_HEADER_SIZE   8
31
32 #define MAX_DPCM (127*127)
33
34
35 typedef struct ROQDPCMContext {
36     short lastSample[2];
37     int input_frames;
38     int buffered_samples;
39     int16_t *frame_buffer;
40     int64_t first_pts;
41 } ROQDPCMContext;
42
43
44 static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
45 {
46     ROQDPCMContext *context = avctx->priv_data;
47
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
57     if (avctx->channels > 2) {
58         av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
59         return AVERROR(EINVAL);
60     }
61     if (avctx->sample_rate != 22050) {
62         av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
63         return AVERROR(EINVAL);
64     }
65
66     avctx->frame_size = ROQ_FRAME_SIZE;
67     avctx->bit_rate   = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
68                         (22050 / ROQ_FRAME_SIZE) * 8;
69
70     context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
71                                       sizeof(*context->frame_buffer));
72     if (!context->frame_buffer)
73         return AVERROR(ENOMEM);
74
75     context->lastSample[0] = context->lastSample[1] = 0;
76
77     return 0;
78 }
79
80 static unsigned char dpcm_predict(short *previous, short current)
81 {
82     int diff;
83     int negative;
84     int result;
85     int predicted;
86
87     diff = current - *previous;
88
89     negative = diff<0;
90     diff = FFABS(diff);
91
92     if (diff >= MAX_DPCM)
93         result = 127;
94     else {
95         result = ff_sqrt(diff);
96         result += diff > result*result+result;
97     }
98
99     /* See if this overflows */
100  retry:
101     diff = result*result;
102     if (negative)
103         diff = -diff;
104     predicted = *previous + diff;
105
106     /* If it overflows, back off a step */
107     if (predicted > 32767 || predicted < -32768) {
108         result--;
109         goto retry;
110     }
111
112     /* Add the sign bit */
113     result |= negative << 7;   //if (negative) result |= 128;
114
115     *previous = predicted;
116
117     return result;
118 }
119
120 static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
121                                  const AVFrame *frame, int *got_packet_ptr)
122 {
123     int i, stereo, data_size, ret;
124     const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
125     uint8_t *out;
126     ROQDPCMContext *context = avctx->priv_data;
127
128     stereo = (avctx->channels == 2);
129
130     if (!in && context->input_frames >= 8)
131         return 0;
132
133     if (in && context->input_frames < 8) {
134         memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
135                in, avctx->frame_size * avctx->channels * sizeof(*in));
136         context->buffered_samples += avctx->frame_size;
137         if (context->input_frames == 0)
138             context->first_pts = frame->pts;
139         if (context->input_frames < 7) {
140             context->input_frames++;
141             return 0;
142         }
143     }
144     if (context->input_frames < 8)
145         in = context->frame_buffer;
146
147     if (stereo) {
148         context->lastSample[0] &= 0xFF00;
149         context->lastSample[1] &= 0xFF00;
150     }
151
152     if (context->input_frames == 7)
153         data_size = avctx->channels * context->buffered_samples;
154     else
155         data_size = avctx->channels * avctx->frame_size;
156
157     if ((ret = ff_alloc_packet2(avctx, avpkt, ROQ_HEADER_SIZE + data_size, 0)) < 0)
158         return ret;
159     out = avpkt->data;
160
161     bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
162     bytestream_put_byte(&out, 0x10);
163     bytestream_put_le32(&out, data_size);
164
165     if (stereo) {
166         bytestream_put_byte(&out, (context->lastSample[1])>>8);
167         bytestream_put_byte(&out, (context->lastSample[0])>>8);
168     } else
169         bytestream_put_le16(&out, context->lastSample[0]);
170
171     /* Write the actual samples */
172     for (i = 0; i < data_size; i++)
173         *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
174
175     avpkt->pts      = context->input_frames <= 7 ? context->first_pts : frame->pts;
176     avpkt->duration = data_size / avctx->channels;
177
178     context->input_frames++;
179     if (!in)
180         context->input_frames = FFMAX(context->input_frames, 8);
181
182     *got_packet_ptr = 1;
183     return 0;
184 }
185
186 const AVCodec ff_roq_dpcm_encoder = {
187     .name           = "roq_dpcm",
188     .long_name      = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
189     .type           = AVMEDIA_TYPE_AUDIO,
190     .id             = AV_CODEC_ID_ROQ_DPCM,
191     .priv_data_size = sizeof(ROQDPCMContext),
192     .init           = roq_dpcm_encode_init,
193     .encode2        = roq_dpcm_encode_frame,
194     .close          = roq_dpcm_encode_close,
195     .capabilities   = AV_CODEC_CAP_DELAY,
196     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
197                                                      AV_SAMPLE_FMT_NONE },
198 };