4 * Copyright (c) 2005 Eric Lasota
5 * Based on RoQ specs (c)2001 Tim Ferguson
7 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/intmath.h"
26 #include "bytestream.h"
28 #define ROQ_FIRST_FRAME_SIZE (735*8)
29 #define ROQ_FRAME_SIZE 735
32 #define MAX_DPCM (127*127)
40 static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
42 ROQDPCMContext *context = avctx->priv_data;
44 if (avctx->channels > 2) {
45 av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
48 if (avctx->sample_rate != 22050) {
49 av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
52 if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
53 av_log(avctx, AV_LOG_ERROR, "Audio must be signed 16-bit\n");
57 avctx->frame_size = ROQ_FIRST_FRAME_SIZE;
59 context->lastSample[0] = context->lastSample[1] = 0;
61 avctx->coded_frame= avcodec_alloc_frame();
62 if (!avctx->coded_frame)
63 return AVERROR(ENOMEM);
68 static unsigned char dpcm_predict(short *previous, short current)
75 diff = current - *previous;
83 result = ff_sqrt(diff);
84 result += diff > result*result+result;
87 /* See if this overflows */
92 predicted = *previous + diff;
94 /* If it overflows, back off a step */
95 if (predicted > 32767 || predicted < -32768) {
100 /* Add the sign bit */
101 result |= negative << 7; //if (negative) result |= 128;
103 *previous = predicted;
108 static int roq_dpcm_encode_frame(AVCodecContext *avctx,
109 unsigned char *frame, int buf_size, void *data)
111 int i, samples, stereo, ch;
115 ROQDPCMContext *context = avctx->priv_data;
117 stereo = (avctx->channels == 2);
120 context->lastSample[0] &= 0xFF00;
121 context->lastSample[1] &= 0xFF00;
127 bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
128 bytestream_put_byte(&out, 0x10);
129 bytestream_put_le32(&out, avctx->frame_size*avctx->channels);
132 bytestream_put_byte(&out, (context->lastSample[1])>>8);
133 bytestream_put_byte(&out, (context->lastSample[0])>>8);
135 bytestream_put_le16(&out, context->lastSample[0]);
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++);
143 /* Use smaller frames from now on */
144 avctx->frame_size = ROQ_FRAME_SIZE;
146 /* Return the result size */
150 static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
152 av_freep(&avctx->coded_frame);
157 AVCodec ff_roq_dpcm_encoder = {
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"),