roqaudioenc.c 4.1 KB

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