libavcodec/roqaudioenc.c
f025588b
 /*
  * RoQ audio encoder
  *
  * Copyright (c) 2005 Eric Lasota
  *    Based on RoQ specs (c)2001 Tim Ferguson
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * 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.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * 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
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "avcodec.h"
 #include "bytestream.h"
32173df3
 #include "internal.h"
9734b8ba
 #include "mathops.h"
f025588b
 
 #define ROQ_FRAME_SIZE           735
cf57c78b
 #define ROQ_HEADER_SIZE   8
f025588b
 
 #define MAX_DPCM (127*127)
 
 
 typedef struct
 {
     short lastSample[2];
cf57c78b
     int input_frames;
     int buffered_samples;
     int16_t *frame_buffer;
32173df3
     int64_t first_pts;
259037d0
 } ROQDPCMContext;
f025588b
 
cf57c78b
 
 static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
 {
     ROQDPCMContext *context = avctx->priv_data;
 
     av_freep(&context->frame_buffer);
 
     return 0;
 }
 
5ef251e5
 static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
f025588b
 {
259037d0
     ROQDPCMContext *context = avctx->priv_data;
cf57c78b
     int ret;
f025588b
 
     if (avctx->channels > 2) {
         av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
f7a2e12f
         return AVERROR(EINVAL);
f025588b
     }
     if (avctx->sample_rate != 22050) {
         av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
f7a2e12f
         return AVERROR(EINVAL);
f025588b
     }
 
cf57c78b
     avctx->frame_size = ROQ_FRAME_SIZE;
3fa9a999
     avctx->bit_rate   = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
                         (22050 / ROQ_FRAME_SIZE) * 8;
cf57c78b
 
     context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
                                       sizeof(*context->frame_buffer));
     if (!context->frame_buffer) {
         ret = AVERROR(ENOMEM);
         goto error;
     }
f025588b
 
     context->lastSample[0] = context->lastSample[1] = 0;
 
     return 0;
cf57c78b
 error:
     roq_dpcm_encode_close(avctx);
     return ret;
f025588b
 }
 
 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;
a0228397
     else {
         result = ff_sqrt(diff);
         result += diff > result*result+result;
     }
f025588b
 
     /* 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;
 }
 
32173df3
 static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                                  const AVFrame *frame, int *got_packet_ptr)
f025588b
 {
32173df3
     int i, stereo, data_size, ret;
     const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
     uint8_t *out;
259037d0
     ROQDPCMContext *context = avctx->priv_data;
f025588b
 
     stereo = (avctx->channels == 2);
 
32173df3
     if (!in && context->input_frames >= 8)
cf57c78b
         return 0;
 
32173df3
     if (in && context->input_frames < 8) {
cf57c78b
         memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
                in, avctx->frame_size * avctx->channels * sizeof(*in));
         context->buffered_samples += avctx->frame_size;
32173df3
         if (context->input_frames == 0)
             context->first_pts = frame->pts;
cf57c78b
         if (context->input_frames < 7) {
             context->input_frames++;
             return 0;
         }
c0b17ea1
     }
     if (context->input_frames < 8) {
cf57c78b
         in = context->frame_buffer;
     }
 
f025588b
     if (stereo) {
         context->lastSample[0] &= 0xFF00;
         context->lastSample[1] &= 0xFF00;
     }
 
a1af505d
     if (context->input_frames == 7)
cf57c78b
         data_size = avctx->channels * context->buffered_samples;
     else
         data_size = avctx->channels * avctx->frame_size;
 
bcaf64b6
     if ((ret = ff_alloc_packet2(avctx, avpkt, ROQ_HEADER_SIZE + data_size)) < 0)
32173df3
         return ret;
     out = avpkt->data;
f025588b
 
     bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
     bytestream_put_byte(&out, 0x10);
cf57c78b
     bytestream_put_le32(&out, data_size);
f025588b
 
     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 */
cf57c78b
     for (i = 0; i < data_size; i++)
         *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
f025588b
 
32173df3
     avpkt->pts      = context->input_frames <= 7 ? context->first_pts : frame->pts;
     avpkt->duration = data_size / avctx->channels;
 
cf57c78b
     context->input_frames++;
32173df3
     if (!in)
cf57c78b
         context->input_frames = FFMAX(context->input_frames, 8);
f025588b
 
32173df3
     *got_packet_ptr = 1;
     return 0;
f025588b
 }
 
e7e2df27
 AVCodec ff_roq_dpcm_encoder = {
ec6402b7
     .name           = "roq_dpcm",
     .type           = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id             = AV_CODEC_ID_ROQ_DPCM,
ec6402b7
     .priv_data_size = sizeof(ROQDPCMContext),
     .init           = roq_dpcm_encode_init,
32173df3
     .encode2        = roq_dpcm_encode_frame,
ec6402b7
     .close          = roq_dpcm_encode_close,
cf57c78b
     .capabilities   = CODEC_CAP_DELAY,
00c3b67b
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                      AV_SAMPLE_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
f025588b
 };