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.

167 lines
4.1KB

  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. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #define ROQ_FIRST_FRAME_SIZE (735*8)
  26. #define ROQ_FRAME_SIZE 735
  27. #define MAX_DPCM (127*127)
  28. typedef struct
  29. {
  30. short lastSample[2];
  31. } ROQDPCMContext;
  32. static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
  33. {
  34. ROQDPCMContext *context = avctx->priv_data;
  35. if (avctx->channels > 2) {
  36. av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
  37. return -1;
  38. }
  39. if (avctx->sample_rate != 22050) {
  40. av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
  41. return -1;
  42. }
  43. if (avctx->sample_fmt != SAMPLE_FMT_S16) {
  44. av_log(avctx, AV_LOG_ERROR, "Audio must be signed 16-bit\n");
  45. return -1;
  46. }
  47. avctx->frame_size = ROQ_FIRST_FRAME_SIZE;
  48. context->lastSample[0] = context->lastSample[1] = 0;
  49. avctx->coded_frame= avcodec_alloc_frame();
  50. avctx->coded_frame->key_frame= 1;
  51. return 0;
  52. }
  53. static unsigned char dpcm_predict(short *previous, short current)
  54. {
  55. int diff;
  56. int negative;
  57. int result;
  58. int predicted;
  59. diff = current - *previous;
  60. negative = diff<0;
  61. diff = FFABS(diff);
  62. if (diff >= MAX_DPCM)
  63. result = 127;
  64. else {
  65. result = ff_sqrt(diff);
  66. result += diff > result*result+result;
  67. }
  68. /* See if this overflows */
  69. retry:
  70. diff = result*result;
  71. if (negative)
  72. diff = -diff;
  73. predicted = *previous + diff;
  74. /* If it overflows, back off a step */
  75. if (predicted > 32767 || predicted < -32768) {
  76. result--;
  77. goto retry;
  78. }
  79. /* Add the sign bit */
  80. result |= negative << 7; //if (negative) result |= 128;
  81. *previous = predicted;
  82. return result;
  83. }
  84. static int roq_dpcm_encode_frame(AVCodecContext *avctx,
  85. unsigned char *frame, int buf_size, void *data)
  86. {
  87. int i, samples, stereo, ch;
  88. short *in;
  89. unsigned char *out;
  90. ROQDPCMContext *context = avctx->priv_data;
  91. stereo = (avctx->channels == 2);
  92. if (stereo) {
  93. context->lastSample[0] &= 0xFF00;
  94. context->lastSample[1] &= 0xFF00;
  95. }
  96. out = frame;
  97. in = data;
  98. bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
  99. bytestream_put_byte(&out, 0x10);
  100. bytestream_put_le32(&out, avctx->frame_size*avctx->channels);
  101. if (stereo) {
  102. bytestream_put_byte(&out, (context->lastSample[1])>>8);
  103. bytestream_put_byte(&out, (context->lastSample[0])>>8);
  104. } else
  105. bytestream_put_le16(&out, context->lastSample[0]);
  106. /* Write the actual samples */
  107. samples = avctx->frame_size;
  108. for (i=0; i<samples; i++)
  109. for (ch=0; ch<avctx->channels; ch++)
  110. *out++ = dpcm_predict(&context->lastSample[ch], *in++);
  111. /* Use smaller frames from now on */
  112. avctx->frame_size = ROQ_FRAME_SIZE;
  113. /* Return the result size */
  114. return out - frame;
  115. }
  116. static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
  117. {
  118. av_freep(&avctx->coded_frame);
  119. return 0;
  120. }
  121. AVCodec roq_dpcm_encoder = {
  122. "roq_dpcm",
  123. CODEC_TYPE_AUDIO,
  124. CODEC_ID_ROQ_DPCM,
  125. sizeof(ROQDPCMContext),
  126. roq_dpcm_encode_init,
  127. roq_dpcm_encode_frame,
  128. roq_dpcm_encode_close,
  129. NULL,
  130. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  131. .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
  132. };