You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.5KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/intmath.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #define ROQ_FRAME_SIZE 735
  27. #define ROQ_HEADER_SIZE 8
  28. #define MAX_DPCM (127*127)
  29. typedef struct
  30. {
  31. short lastSample[2];
  32. int input_frames;
  33. int buffered_samples;
  34. int16_t *frame_buffer;
  35. } ROQDPCMContext;
  36. static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
  37. {
  38. ROQDPCMContext *context = avctx->priv_data;
  39. av_freep(&avctx->coded_frame);
  40. av_freep(&context->frame_buffer);
  41. return 0;
  42. }
  43. static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
  44. {
  45. ROQDPCMContext *context = avctx->priv_data;
  46. int ret;
  47. if (avctx->channels > 2) {
  48. av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
  49. return AVERROR(EINVAL);
  50. }
  51. if (avctx->sample_rate != 22050) {
  52. av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
  53. return AVERROR(EINVAL);
  54. }
  55. avctx->frame_size = ROQ_FRAME_SIZE;
  56. avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
  57. (22050 / ROQ_FRAME_SIZE) * 8;
  58. context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
  59. sizeof(*context->frame_buffer));
  60. if (!context->frame_buffer) {
  61. ret = AVERROR(ENOMEM);
  62. goto error;
  63. }
  64. context->lastSample[0] = context->lastSample[1] = 0;
  65. avctx->coded_frame= avcodec_alloc_frame();
  66. if (!avctx->coded_frame) {
  67. ret = AVERROR(ENOMEM);
  68. goto error;
  69. }
  70. return 0;
  71. error:
  72. roq_dpcm_encode_close(avctx);
  73. return ret;
  74. }
  75. static unsigned char dpcm_predict(short *previous, short current)
  76. {
  77. int diff;
  78. int negative;
  79. int result;
  80. int predicted;
  81. diff = current - *previous;
  82. negative = diff<0;
  83. diff = FFABS(diff);
  84. if (diff >= MAX_DPCM)
  85. result = 127;
  86. else {
  87. result = ff_sqrt(diff);
  88. result += diff > result*result+result;
  89. }
  90. /* See if this overflows */
  91. retry:
  92. diff = result*result;
  93. if (negative)
  94. diff = -diff;
  95. predicted = *previous + diff;
  96. /* If it overflows, back off a step */
  97. if (predicted > 32767 || predicted < -32768) {
  98. result--;
  99. goto retry;
  100. }
  101. /* Add the sign bit */
  102. result |= negative << 7; //if (negative) result |= 128;
  103. *previous = predicted;
  104. return result;
  105. }
  106. static int roq_dpcm_encode_frame(AVCodecContext *avctx,
  107. unsigned char *frame, int buf_size, void *data)
  108. {
  109. int i, stereo, data_size;
  110. const int16_t *in = data;
  111. uint8_t *out = frame;
  112. ROQDPCMContext *context = avctx->priv_data;
  113. stereo = (avctx->channels == 2);
  114. if (!data && context->input_frames >= 8)
  115. return 0;
  116. if (data && context->input_frames < 8) {
  117. memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
  118. in, avctx->frame_size * avctx->channels * sizeof(*in));
  119. context->buffered_samples += avctx->frame_size;
  120. if (context->input_frames < 7) {
  121. context->input_frames++;
  122. return 0;
  123. }
  124. in = context->frame_buffer;
  125. }
  126. if (stereo) {
  127. context->lastSample[0] &= 0xFF00;
  128. context->lastSample[1] &= 0xFF00;
  129. }
  130. if (context->input_frames == 7 || !data)
  131. data_size = avctx->channels * context->buffered_samples;
  132. else
  133. data_size = avctx->channels * avctx->frame_size;
  134. if (buf_size < ROQ_HEADER_SIZE + data_size) {
  135. av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
  136. return AVERROR(EINVAL);
  137. }
  138. bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
  139. bytestream_put_byte(&out, 0x10);
  140. bytestream_put_le32(&out, data_size);
  141. if (stereo) {
  142. bytestream_put_byte(&out, (context->lastSample[1])>>8);
  143. bytestream_put_byte(&out, (context->lastSample[0])>>8);
  144. } else
  145. bytestream_put_le16(&out, context->lastSample[0]);
  146. /* Write the actual samples */
  147. for (i = 0; i < data_size; i++)
  148. *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
  149. context->input_frames++;
  150. if (!data)
  151. context->input_frames = FFMAX(context->input_frames, 8);
  152. /* Return the result size */
  153. return ROQ_HEADER_SIZE + data_size;
  154. }
  155. AVCodec ff_roq_dpcm_encoder = {
  156. .name = "roq_dpcm",
  157. .type = AVMEDIA_TYPE_AUDIO,
  158. .id = CODEC_ID_ROQ_DPCM,
  159. .priv_data_size = sizeof(ROQDPCMContext),
  160. .init = roq_dpcm_encode_init,
  161. .encode = roq_dpcm_encode_frame,
  162. .close = roq_dpcm_encode_close,
  163. .capabilities = CODEC_CAP_DELAY,
  164. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  165. .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
  166. };