roqaudioenc.c 5.77 KB
Newer Older
Vitor Sessak's avatar
Vitor Sessak committed
1 2 3 4 5 6
/*
 * RoQ audio encoder
 *
 * Copyright (c) 2005 Eric Lasota
 *    Based on RoQ specs (c)2001 Tim Ferguson
 *
7
 * This file is part of Libav.
Vitor Sessak's avatar
Vitor Sessak committed
8
 *
9
 * Libav is free software; you can redistribute it and/or
Vitor Sessak's avatar
Vitor Sessak committed
10 11 12 13
 * 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.
 *
14
 * Libav is distributed in the hope that it will be useful,
Vitor Sessak's avatar
Vitor Sessak committed
15 16 17 18 19
 * 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
20
 * License along with Libav; if not, write to the Free Software
Vitor Sessak's avatar
Vitor Sessak committed
21 22 23 24 25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avcodec.h"
#include "bytestream.h"
26
#include "internal.h"
27
#include "mathops.h"
Vitor Sessak's avatar
Vitor Sessak committed
28 29

#define ROQ_FRAME_SIZE           735
30
#define ROQ_HEADER_SIZE   8
Vitor Sessak's avatar
Vitor Sessak committed
31 32 33 34

#define MAX_DPCM (127*127)


35
typedef struct ROQDPCMContext {
Vitor Sessak's avatar
Vitor Sessak committed
36
    short lastSample[2];
37 38 39
    int input_frames;
    int buffered_samples;
    int16_t *frame_buffer;
40
    int64_t first_pts;
41
} ROQDPCMContext;
Vitor Sessak's avatar
Vitor Sessak committed
42

43 44 45 46 47 48 49 50 51 52

static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
{
    ROQDPCMContext *context = avctx->priv_data;

    av_freep(&context->frame_buffer);

    return 0;
}

53
static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
Vitor Sessak's avatar
Vitor Sessak committed
54
{
55
    ROQDPCMContext *context = avctx->priv_data;
56
    int ret;
Vitor Sessak's avatar
Vitor Sessak committed
57 58 59

    if (avctx->channels > 2) {
        av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
60
        return AVERROR(EINVAL);
Vitor Sessak's avatar
Vitor Sessak committed
61 62 63
    }
    if (avctx->sample_rate != 22050) {
        av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
64
        return AVERROR(EINVAL);
Vitor Sessak's avatar
Vitor Sessak committed
65 66
    }

67
    avctx->frame_size = ROQ_FRAME_SIZE;
68 69
    avctx->bit_rate   = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
                        (22050 / ROQ_FRAME_SIZE) * 8;
70 71 72 73 74 75 76

    context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
                                      sizeof(*context->frame_buffer));
    if (!context->frame_buffer) {
        ret = AVERROR(ENOMEM);
        goto error;
    }
Vitor Sessak's avatar
Vitor Sessak committed
77 78 79 80

    context->lastSample[0] = context->lastSample[1] = 0;

    return 0;
81 82 83
error:
    roq_dpcm_encode_close(avctx);
    return ret;
Vitor Sessak's avatar
Vitor Sessak committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}

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;
100 101 102 103
    else {
        result = ff_sqrt(diff);
        result += diff > result*result+result;
    }
Vitor Sessak's avatar
Vitor Sessak committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    /* 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;
}

126 127
static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                                 const AVFrame *frame, int *got_packet_ptr)
Vitor Sessak's avatar
Vitor Sessak committed
128
{
129 130 131
    int i, stereo, data_size, ret;
    const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
    uint8_t *out;
132
    ROQDPCMContext *context = avctx->priv_data;
Vitor Sessak's avatar
Vitor Sessak committed
133 134 135

    stereo = (avctx->channels == 2);

136
    if (!in && context->input_frames >= 8)
137 138
        return 0;

139
    if (in && context->input_frames < 8) {
140 141 142
        memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
               in, avctx->frame_size * avctx->channels * sizeof(*in));
        context->buffered_samples += avctx->frame_size;
143 144
        if (context->input_frames == 0)
            context->first_pts = frame->pts;
145 146 147 148 149
        if (context->input_frames < 7) {
            context->input_frames++;
            return 0;
        }
    }
150 151
    if (context->input_frames < 8)
        in = context->frame_buffer;
152

Vitor Sessak's avatar
Vitor Sessak committed
153 154 155 156 157
    if (stereo) {
        context->lastSample[0] &= 0xFF00;
        context->lastSample[1] &= 0xFF00;
    }

158
    if (context->input_frames == 7)
159 160 161 162
        data_size = avctx->channels * context->buffered_samples;
    else
        data_size = avctx->channels * avctx->frame_size;

163 164 165
    if ((ret = ff_alloc_packet(avpkt, ROQ_HEADER_SIZE + data_size))) {
        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
        return ret;
166
    }
167
    out = avpkt->data;
Vitor Sessak's avatar
Vitor Sessak committed
168 169 170

    bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
    bytestream_put_byte(&out, 0x10);
171
    bytestream_put_le32(&out, data_size);
Vitor Sessak's avatar
Vitor Sessak committed
172 173 174 175 176 177 178 179

    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 */
180 181
    for (i = 0; i < data_size; i++)
        *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
Vitor Sessak's avatar
Vitor Sessak committed
182

183 184 185
    avpkt->pts      = context->input_frames <= 7 ? context->first_pts : frame->pts;
    avpkt->duration = data_size / avctx->channels;

186
    context->input_frames++;
187
    if (!in)
188
        context->input_frames = FFMAX(context->input_frames, 8);
Vitor Sessak's avatar
Vitor Sessak committed
189

190 191
    *got_packet_ptr = 1;
    return 0;
Vitor Sessak's avatar
Vitor Sessak committed
192 193
}

194
AVCodec ff_roq_dpcm_encoder = {
195
    .name           = "roq_dpcm",
196
    .long_name      = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
197
    .type           = AVMEDIA_TYPE_AUDIO,
198
    .id             = AV_CODEC_ID_ROQ_DPCM,
199 200
    .priv_data_size = sizeof(ROQDPCMContext),
    .init           = roq_dpcm_encode_init,
201
    .encode2        = roq_dpcm_encode_frame,
202
    .close          = roq_dpcm_encode_close,
203
    .capabilities   = AV_CODEC_CAP_DELAY,
204 205
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                     AV_SAMPLE_FMT_NONE },
Vitor Sessak's avatar
Vitor Sessak committed
206
};