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
25 #include "bytestream.h"
29 #define ROQ_FRAME_SIZE 735
30 #define ROQ_HEADER_SIZE 8
32 #define MAX_DPCM (127*127)
35 typedef struct ROQDPCMContext {
39 int16_t *frame_buffer;
44 static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
46 ROQDPCMContext *context = avctx->priv_data;
48 av_freep(&context->frame_buffer);
53 static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
55 ROQDPCMContext *context = avctx->priv_data;
58 if (avctx->channels > 2) {
59 av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
60 return AVERROR(EINVAL);
62 if (avctx->sample_rate != 22050) {
63 av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
64 return AVERROR(EINVAL);
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;
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);
78 context->lastSample[0] = context->lastSample[1] = 0;
82 roq_dpcm_encode_close(avctx);
86 static unsigned char dpcm_predict(short *previous, short current)
93 diff = current - *previous;
101 result = ff_sqrt(diff);
102 result += diff > result*result+result;
105 /* See if this overflows */
107 diff = result*result;
110 predicted = *previous + diff;
112 /* If it overflows, back off a step */
113 if (predicted > 32767 || predicted < -32768) {
118 /* Add the sign bit */
119 result |= negative << 7; //if (negative) result |= 128;
121 *previous = predicted;
126 static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
127 const AVFrame *frame, int *got_packet_ptr)
129 int i, stereo, data_size, ret;
130 const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
132 ROQDPCMContext *context = avctx->priv_data;
134 stereo = (avctx->channels == 2);
136 if (!in && context->input_frames >= 8)
139 if (in && context->input_frames < 8) {
140 memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
141 in, avctx->frame_size * avctx->channels * sizeof(*in));
142 context->buffered_samples += avctx->frame_size;
143 if (context->input_frames == 0)
144 context->first_pts = frame->pts;
145 if (context->input_frames < 7) {
146 context->input_frames++;
150 if (context->input_frames < 8)
151 in = context->frame_buffer;
154 context->lastSample[0] &= 0xFF00;
155 context->lastSample[1] &= 0xFF00;
158 if (context->input_frames == 7)
159 data_size = avctx->channels * context->buffered_samples;
161 data_size = avctx->channels * avctx->frame_size;
163 if ((ret = ff_alloc_packet2(avctx, avpkt, ROQ_HEADER_SIZE + data_size, 0)) < 0)
167 bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
168 bytestream_put_byte(&out, 0x10);
169 bytestream_put_le32(&out, data_size);
172 bytestream_put_byte(&out, (context->lastSample[1])>>8);
173 bytestream_put_byte(&out, (context->lastSample[0])>>8);
175 bytestream_put_le16(&out, context->lastSample[0]);
177 /* Write the actual samples */
178 for (i = 0; i < data_size; i++)
179 *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
181 avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
182 avpkt->duration = data_size / avctx->channels;
184 context->input_frames++;
186 context->input_frames = FFMAX(context->input_frames, 8);
192 AVCodec ff_roq_dpcm_encoder = {
194 .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
195 .type = AVMEDIA_TYPE_AUDIO,
196 .id = AV_CODEC_ID_ROQ_DPCM,
197 .priv_data_size = sizeof(ROQDPCMContext),
198 .init = roq_dpcm_encode_init,
199 .encode2 = roq_dpcm_encode_frame,
200 .close = roq_dpcm_encode_close,
201 .capabilities = AV_CODEC_CAP_DELAY,
202 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
203 AV_SAMPLE_FMT_NONE },