roqaudioenc.c 4.4 KB

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