libavcodec/roqaudioenc.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * RoQ audio encoder | ||
| 3 | * | ||
| 4 | * Copyright (c) 2005 Eric Lasota | ||
| 5 | * Based on RoQ specs (c)2001 Tim Ferguson | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 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. | ||
| 13 | * | ||
| 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. | ||
| 18 | * | ||
| 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 | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include "libavutil/mem.h" | ||
| 25 | #include "avcodec.h" | ||
| 26 | #include "bytestream.h" | ||
| 27 | #include "codec_internal.h" | ||
| 28 | #include "encode.h" | ||
| 29 | #include "mathops.h" | ||
| 30 | |||
| 31 | #define ROQ_FRAME_SIZE 735 | ||
| 32 | #define ROQ_HEADER_SIZE 8 | ||
| 33 | |||
| 34 | #define MAX_DPCM (127*127) | ||
| 35 | |||
| 36 | |||
| 37 | typedef struct ROQDPCMContext { | ||
| 38 | short lastSample[2]; | ||
| 39 | int input_frames; | ||
| 40 | int buffered_samples; | ||
| 41 | int16_t *frame_buffer; | ||
| 42 | int64_t first_pts; | ||
| 43 | } ROQDPCMContext; | ||
| 44 | |||
| 45 | |||
| 46 | 2 | static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx) | |
| 47 | { | ||
| 48 | 2 | ROQDPCMContext *context = avctx->priv_data; | |
| 49 | |||
| 50 | 2 | av_freep(&context->frame_buffer); | |
| 51 | |||
| 52 | 2 | return 0; | |
| 53 | } | ||
| 54 | |||
| 55 | 2 | static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx) | |
| 56 | { | ||
| 57 | 2 | ROQDPCMContext *context = avctx->priv_data; | |
| 58 | 2 | int channels = avctx->ch_layout.nb_channels; | |
| 59 | |||
| 60 | 2 | if (channels > 2) { | |
| 61 | ✗ | av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n"); | |
| 62 | ✗ | return AVERROR(EINVAL); | |
| 63 | } | ||
| 64 | 2 | if (avctx->sample_rate != 22050) { | |
| 65 | ✗ | av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n"); | |
| 66 | ✗ | return AVERROR(EINVAL); | |
| 67 | } | ||
| 68 | |||
| 69 | 2 | avctx->frame_size = ROQ_FRAME_SIZE; | |
| 70 | 2 | avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * channels) * | |
| 71 | 2 | (22050 / ROQ_FRAME_SIZE) * 8; | |
| 72 | |||
| 73 | 2 | context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * channels * | |
| 74 | sizeof(*context->frame_buffer)); | ||
| 75 | 2 | if (!context->frame_buffer) | |
| 76 | ✗ | return AVERROR(ENOMEM); | |
| 77 | |||
| 78 | 2 | context->lastSample[0] = context->lastSample[1] = 0; | |
| 79 | |||
| 80 | 2 | return 0; | |
| 81 | } | ||
| 82 | |||
| 83 | 266805 | static unsigned char dpcm_predict(short *previous, short current) | |
| 84 | { | ||
| 85 | int diff; | ||
| 86 | int negative; | ||
| 87 | int result; | ||
| 88 | int predicted; | ||
| 89 | |||
| 90 | 266805 | diff = current - *previous; | |
| 91 | |||
| 92 | 266805 | negative = diff<0; | |
| 93 | 266805 | diff = FFABS(diff); | |
| 94 | |||
| 95 | 266805 | if (diff >= MAX_DPCM) | |
| 96 | 10896 | result = 127; | |
| 97 | else { | ||
| 98 | 255909 | result = ff_sqrt(diff); | |
| 99 | 255909 | result += diff > result*result+result; | |
| 100 | } | ||
| 101 | |||
| 102 | /* See if this overflows */ | ||
| 103 | 266805 | retry: | |
| 104 | 266805 | diff = result*result; | |
| 105 | 266805 | if (negative) | |
| 106 | 132707 | diff = -diff; | |
| 107 | 266805 | predicted = *previous + diff; | |
| 108 | |||
| 109 | /* If it overflows, back off a step */ | ||
| 110 | 266805 | if (predicted > 32767 || predicted < -32768) { | |
| 111 | ✗ | result--; | |
| 112 | ✗ | goto retry; | |
| 113 | } | ||
| 114 | |||
| 115 | /* Add the sign bit */ | ||
| 116 | 266805 | result |= negative << 7; //if (negative) result |= 128; | |
| 117 | |||
| 118 | 266805 | *previous = predicted; | |
| 119 | |||
| 120 | 266805 | return result; | |
| 121 | } | ||
| 122 | |||
| 123 | 186 | static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, | |
| 124 | const AVFrame *frame, int *got_packet_ptr) | ||
| 125 | { | ||
| 126 | int i, stereo, data_size, ret; | ||
| 127 | 186 | const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL; | |
| 128 | 186 | int channels = avctx->ch_layout.nb_channels; | |
| 129 | uint8_t *out; | ||
| 130 | 186 | ROQDPCMContext *context = avctx->priv_data; | |
| 131 | |||
| 132 | 186 | stereo = (channels == 2); | |
| 133 | |||
| 134 | 186 | if (!in && (context->input_frames >= 8 || !context->buffered_samples)) | |
| 135 | 2 | return 0; | |
| 136 | |||
| 137 | 184 | if (in && context->input_frames < 8) { | |
| 138 | 11 | memcpy(&context->frame_buffer[context->buffered_samples * channels], | |
| 139 | 11 | in, avctx->frame_size * channels * sizeof(*in)); | |
| 140 | 11 | context->buffered_samples += avctx->frame_size; | |
| 141 | 11 | if (context->input_frames == 0) | |
| 142 | 2 | context->first_pts = frame->pts; | |
| 143 | 11 | if (context->input_frames < 7) { | |
| 144 | 10 | context->input_frames++; | |
| 145 | 10 | return 0; | |
| 146 | } | ||
| 147 | } | ||
| 148 | 174 | if (context->input_frames < 8) | |
| 149 | 2 | in = context->frame_buffer; | |
| 150 | |||
| 151 | 174 | if (stereo) { | |
| 152 | 173 | context->lastSample[0] &= 0xFF00; | |
| 153 | 173 | context->lastSample[1] &= 0xFF00; | |
| 154 | } | ||
| 155 | |||
| 156 | 174 | if (context->input_frames < 8) | |
| 157 | 2 | data_size = channels * context->buffered_samples; | |
| 158 | else | ||
| 159 | 172 | data_size = channels * avctx->frame_size; | |
| 160 | |||
| 161 | 174 | ret = ff_get_encode_buffer(avctx, avpkt, ROQ_HEADER_SIZE + data_size, 0); | |
| 162 | 174 | if (ret < 0) | |
| 163 | ✗ | return ret; | |
| 164 | 174 | out = avpkt->data; | |
| 165 | |||
| 166 | 174 | bytestream_put_byte(&out, stereo ? 0x21 : 0x20); | |
| 167 | 174 | bytestream_put_byte(&out, 0x10); | |
| 168 | 174 | bytestream_put_le32(&out, data_size); | |
| 169 | |||
| 170 | 174 | if (stereo) { | |
| 171 | 173 | bytestream_put_byte(&out, (context->lastSample[1])>>8); | |
| 172 | 173 | bytestream_put_byte(&out, (context->lastSample[0])>>8); | |
| 173 | } else | ||
| 174 | 1 | bytestream_put_le16(&out, context->lastSample[0]); | |
| 175 | |||
| 176 | /* Write the actual samples */ | ||
| 177 | 266979 | for (i = 0; i < data_size; i++) | |
| 178 | 266805 | *out++ = dpcm_predict(&context->lastSample[(i & 1) & stereo], *in++); | |
| 179 | |||
| 180 | 174 | avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts; | |
| 181 | 174 | avpkt->duration = data_size / channels; | |
| 182 | |||
| 183 | 174 | context->input_frames++; | |
| 184 | 174 | if (!frame) | |
| 185 | 1 | context->input_frames = FFMAX(context->input_frames, 8); | |
| 186 | |||
| 187 | 174 | *got_packet_ptr = 1; | |
| 188 | 174 | return 0; | |
| 189 | } | ||
| 190 | |||
| 191 | const FFCodec ff_roq_dpcm_encoder = { | ||
| 192 | .p.name = "roq_dpcm", | ||
| 193 | CODEC_LONG_NAME("id RoQ DPCM"), | ||
| 194 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 195 | .p.id = AV_CODEC_ID_ROQ_DPCM, | ||
| 196 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, | ||
| 197 | .priv_data_size = sizeof(ROQDPCMContext), | ||
| 198 | .init = roq_dpcm_encode_init, | ||
| 199 | FF_CODEC_ENCODE_CB(roq_dpcm_encode_frame), | ||
| 200 | .close = roq_dpcm_encode_close, | ||
| 201 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16), | ||
| 202 | }; | ||
| 203 |