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.

207 lines
5.8KB

  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 "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. #include "mathops.h"
  27. #define ROQ_FRAME_SIZE 735
  28. #define ROQ_HEADER_SIZE 8
  29. #define MAX_DPCM (127*127)
  30. typedef struct ROQDPCMContext {
  31. short lastSample[2];
  32. int input_frames;
  33. int buffered_samples;
  34. int16_t *frame_buffer;
  35. int64_t first_pts;
  36. } ROQDPCMContext;
  37. static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
  38. {
  39. ROQDPCMContext *context = avctx->priv_data;
  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. return 0;
  66. error:
  67. roq_dpcm_encode_close(avctx);
  68. return ret;
  69. }
  70. static unsigned char dpcm_predict(short *previous, short current)
  71. {
  72. int diff;
  73. int negative;
  74. int result;
  75. int predicted;
  76. diff = current - *previous;
  77. negative = diff<0;
  78. diff = FFABS(diff);
  79. if (diff >= MAX_DPCM)
  80. result = 127;
  81. else {
  82. result = ff_sqrt(diff);
  83. result += diff > result*result+result;
  84. }
  85. /* See if this overflows */
  86. retry:
  87. diff = result*result;
  88. if (negative)
  89. diff = -diff;
  90. predicted = *previous + diff;
  91. /* If it overflows, back off a step */
  92. if (predicted > 32767 || predicted < -32768) {
  93. result--;
  94. goto retry;
  95. }
  96. /* Add the sign bit */
  97. result |= negative << 7; //if (negative) result |= 128;
  98. *previous = predicted;
  99. return result;
  100. }
  101. static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  102. const AVFrame *frame, int *got_packet_ptr)
  103. {
  104. int i, stereo, data_size, ret;
  105. const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
  106. uint8_t *out;
  107. ROQDPCMContext *context = avctx->priv_data;
  108. stereo = (avctx->channels == 2);
  109. if (!in && context->input_frames >= 8)
  110. return 0;
  111. if (in && context->input_frames < 8) {
  112. memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
  113. in, avctx->frame_size * avctx->channels * sizeof(*in));
  114. context->buffered_samples += avctx->frame_size;
  115. if (context->input_frames == 0)
  116. context->first_pts = frame->pts;
  117. if (context->input_frames < 7) {
  118. context->input_frames++;
  119. return 0;
  120. }
  121. }
  122. if (context->input_frames < 8)
  123. in = context->frame_buffer;
  124. if (stereo) {
  125. context->lastSample[0] &= 0xFF00;
  126. context->lastSample[1] &= 0xFF00;
  127. }
  128. if (context->input_frames == 7)
  129. data_size = avctx->channels * context->buffered_samples;
  130. else
  131. data_size = avctx->channels * avctx->frame_size;
  132. if ((ret = ff_alloc_packet(avpkt, ROQ_HEADER_SIZE + data_size))) {
  133. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  134. return ret;
  135. }
  136. out = avpkt->data;
  137. bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
  138. bytestream_put_byte(&out, 0x10);
  139. bytestream_put_le32(&out, data_size);
  140. if (stereo) {
  141. bytestream_put_byte(&out, (context->lastSample[1])>>8);
  142. bytestream_put_byte(&out, (context->lastSample[0])>>8);
  143. } else
  144. bytestream_put_le16(&out, context->lastSample[0]);
  145. /* Write the actual samples */
  146. for (i = 0; i < data_size; i++)
  147. *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
  148. avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
  149. avpkt->duration = data_size / avctx->channels;
  150. context->input_frames++;
  151. if (!in)
  152. context->input_frames = FFMAX(context->input_frames, 8);
  153. *got_packet_ptr = 1;
  154. return 0;
  155. }
  156. AVCodec ff_roq_dpcm_encoder = {
  157. .name = "roq_dpcm",
  158. .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
  159. .type = AVMEDIA_TYPE_AUDIO,
  160. .id = AV_CODEC_ID_ROQ_DPCM,
  161. .priv_data_size = sizeof(ROQDPCMContext),
  162. .init = roq_dpcm_encode_init,
  163. .encode2 = roq_dpcm_encode_frame,
  164. .close = roq_dpcm_encode_close,
  165. .capabilities = AV_CODEC_CAP_DELAY,
  166. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  167. AV_SAMPLE_FMT_NONE },
  168. };