2007-05-07 08:55:27 +00:00
|
|
|
/*
|
|
|
|
* RoQ audio encoder
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Eric Lasota
|
|
|
|
* Based on RoQ specs (c)2001 Tim Ferguson
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2007-05-07 08:55:27 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-05-07 08:55:27 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-05-07 08:55:27 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-05-07 08:55:27 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2010-03-08 21:19:56 +00:00
|
|
|
#include "libavutil/intmath.h"
|
2007-05-07 08:55:27 +00:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2012-02-28 01:53:09 +00:00
|
|
|
#include "internal.h"
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
#define ROQ_FRAME_SIZE 735
|
2012-02-22 21:20:49 +00:00
|
|
|
#define ROQ_HEADER_SIZE 8
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
#define MAX_DPCM (127*127)
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
short lastSample[2];
|
2012-02-22 21:20:49 +00:00
|
|
|
int input_frames;
|
|
|
|
int buffered_samples;
|
|
|
|
int16_t *frame_buffer;
|
2012-02-28 01:53:09 +00:00
|
|
|
int64_t first_pts;
|
2008-12-11 19:27:53 +00:00
|
|
|
} ROQDPCMContext;
|
2007-05-07 08:55:27 +00:00
|
|
|
|
2012-02-22 21:20:49 +00:00
|
|
|
|
|
|
|
static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
ROQDPCMContext *context = avctx->priv_data;
|
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
#if FF_API_OLD_ENCODE_AUDIO
|
2012-02-22 21:20:49 +00:00
|
|
|
av_freep(&avctx->coded_frame);
|
2012-02-28 01:53:09 +00:00
|
|
|
#endif
|
2012-02-22 21:20:49 +00:00
|
|
|
av_freep(&context->frame_buffer);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-22 13:48:55 +00:00
|
|
|
static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
|
2007-05-07 08:55:27 +00:00
|
|
|
{
|
2008-12-11 19:27:53 +00:00
|
|
|
ROQDPCMContext *context = avctx->priv_data;
|
2012-02-22 21:20:49 +00:00
|
|
|
int ret;
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
if (avctx->channels > 2) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
|
2012-02-22 23:33:39 +00:00
|
|
|
return AVERROR(EINVAL);
|
2007-05-07 08:55:27 +00:00
|
|
|
}
|
|
|
|
if (avctx->sample_rate != 22050) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
|
2012-02-22 23:33:39 +00:00
|
|
|
return AVERROR(EINVAL);
|
2007-05-07 08:55:27 +00:00
|
|
|
}
|
|
|
|
|
2012-02-22 21:20:49 +00:00
|
|
|
avctx->frame_size = ROQ_FRAME_SIZE;
|
2012-02-22 23:32:19 +00:00
|
|
|
avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
|
|
|
|
(22050 / ROQ_FRAME_SIZE) * 8;
|
2012-02-22 21:20:49 +00:00
|
|
|
|
|
|
|
context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
|
|
|
|
sizeof(*context->frame_buffer));
|
|
|
|
if (!context->frame_buffer) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto error;
|
|
|
|
}
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
context->lastSample[0] = context->lastSample[1] = 0;
|
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
#if FF_API_OLD_ENCODE_AUDIO
|
2007-05-07 08:55:27 +00:00
|
|
|
avctx->coded_frame= avcodec_alloc_frame();
|
2012-02-22 21:20:49 +00:00
|
|
|
if (!avctx->coded_frame) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto error;
|
|
|
|
}
|
2012-02-28 01:53:09 +00:00
|
|
|
#endif
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-02-22 21:20:49 +00:00
|
|
|
error:
|
|
|
|
roq_dpcm_encode_close(avctx);
|
|
|
|
return ret;
|
2007-05-07 08:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char dpcm_predict(short *previous, short current)
|
|
|
|
{
|
|
|
|
int diff;
|
|
|
|
int negative;
|
|
|
|
int result;
|
|
|
|
int predicted;
|
|
|
|
|
|
|
|
diff = current - *previous;
|
|
|
|
|
|
|
|
negative = diff<0;
|
|
|
|
diff = FFABS(diff);
|
|
|
|
|
|
|
|
if (diff >= MAX_DPCM)
|
|
|
|
result = 127;
|
2009-10-18 16:29:10 +00:00
|
|
|
else {
|
|
|
|
result = ff_sqrt(diff);
|
|
|
|
result += diff > result*result+result;
|
|
|
|
}
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
/* See if this overflows */
|
|
|
|
retry:
|
|
|
|
diff = result*result;
|
|
|
|
if (negative)
|
|
|
|
diff = -diff;
|
|
|
|
predicted = *previous + diff;
|
|
|
|
|
|
|
|
/* If it overflows, back off a step */
|
|
|
|
if (predicted > 32767 || predicted < -32768) {
|
|
|
|
result--;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the sign bit */
|
|
|
|
result |= negative << 7; //if (negative) result |= 128;
|
|
|
|
|
|
|
|
*previous = predicted;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
|
|
|
|
const AVFrame *frame, int *got_packet_ptr)
|
2007-05-07 08:55:27 +00:00
|
|
|
{
|
2012-02-28 01:53:09 +00:00
|
|
|
int i, stereo, data_size, ret;
|
|
|
|
const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
|
|
|
|
uint8_t *out;
|
2008-12-11 19:27:53 +00:00
|
|
|
ROQDPCMContext *context = avctx->priv_data;
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
stereo = (avctx->channels == 2);
|
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
if (!in && context->input_frames >= 8)
|
2012-02-22 21:20:49 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
if (in && context->input_frames < 8) {
|
2012-02-22 21:20:49 +00:00
|
|
|
memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
|
|
|
|
in, avctx->frame_size * avctx->channels * sizeof(*in));
|
|
|
|
context->buffered_samples += avctx->frame_size;
|
2012-02-28 01:53:09 +00:00
|
|
|
if (context->input_frames == 0)
|
|
|
|
context->first_pts = frame->pts;
|
2012-02-22 21:20:49 +00:00
|
|
|
if (context->input_frames < 7) {
|
|
|
|
context->input_frames++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
in = context->frame_buffer;
|
|
|
|
}
|
|
|
|
|
2007-05-07 08:55:27 +00:00
|
|
|
if (stereo) {
|
|
|
|
context->lastSample[0] &= 0xFF00;
|
|
|
|
context->lastSample[1] &= 0xFF00;
|
|
|
|
}
|
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
if (context->input_frames == 7 || !in)
|
2012-02-22 21:20:49 +00:00
|
|
|
data_size = avctx->channels * context->buffered_samples;
|
|
|
|
else
|
|
|
|
data_size = avctx->channels * avctx->frame_size;
|
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
if ((ret = ff_alloc_packet(avpkt, ROQ_HEADER_SIZE + data_size))) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
|
|
|
|
return ret;
|
2012-02-22 21:20:49 +00:00
|
|
|
}
|
2012-02-28 01:53:09 +00:00
|
|
|
out = avpkt->data;
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
|
|
|
|
bytestream_put_byte(&out, 0x10);
|
2012-02-22 21:20:49 +00:00
|
|
|
bytestream_put_le32(&out, data_size);
|
2007-05-07 08:55:27 +00:00
|
|
|
|
|
|
|
if (stereo) {
|
|
|
|
bytestream_put_byte(&out, (context->lastSample[1])>>8);
|
|
|
|
bytestream_put_byte(&out, (context->lastSample[0])>>8);
|
|
|
|
} else
|
|
|
|
bytestream_put_le16(&out, context->lastSample[0]);
|
|
|
|
|
|
|
|
/* Write the actual samples */
|
2012-02-22 21:20:49 +00:00
|
|
|
for (i = 0; i < data_size; i++)
|
|
|
|
*out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
|
2007-05-07 08:55:27 +00:00
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
|
|
|
|
avpkt->duration = data_size / avctx->channels;
|
|
|
|
|
2012-02-22 21:20:49 +00:00
|
|
|
context->input_frames++;
|
2012-02-28 01:53:09 +00:00
|
|
|
if (!in)
|
2012-02-22 21:20:49 +00:00
|
|
|
context->input_frames = FFMAX(context->input_frames, 8);
|
2007-05-07 08:55:27 +00:00
|
|
|
|
2012-02-28 01:53:09 +00:00
|
|
|
*got_packet_ptr = 1;
|
|
|
|
return 0;
|
2007-05-07 08:55:27 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodec ff_roq_dpcm_encoder = {
|
2011-07-17 10:54:31 +00:00
|
|
|
.name = "roq_dpcm",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.id = CODEC_ID_ROQ_DPCM,
|
|
|
|
.priv_data_size = sizeof(ROQDPCMContext),
|
|
|
|
.init = roq_dpcm_encode_init,
|
2012-02-28 01:53:09 +00:00
|
|
|
.encode2 = roq_dpcm_encode_frame,
|
2011-07-17 10:54:31 +00:00
|
|
|
.close = roq_dpcm_encode_close,
|
2012-02-22 21:20:49 +00:00
|
|
|
.capabilities = CODEC_CAP_DELAY,
|
2010-11-12 11:04:40 +00:00
|
|
|
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
|
2008-06-12 21:50:13 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
|
2007-05-07 08:55:27 +00:00
|
|
|
};
|