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.7KB

  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
  31. {
  32. short lastSample[2];
  33. int input_frames;
  34. int buffered_samples;
  35. int16_t *frame_buffer;
  36. int64_t first_pts;
  37. } ROQDPCMContext;
  38. static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
  39. {
  40. ROQDPCMContext *context = avctx->priv_data;
  41. av_freep(&context->frame_buffer);
  42. return 0;
  43. }
  44. static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
  45. {
  46. ROQDPCMContext *context = avctx->priv_data;
  47. int ret;
  48. if (avctx->channels > 2) {
  49. av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
  50. return AVERROR(EINVAL);
  51. }
  52. if (avctx->sample_rate != 22050) {
  53. av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
  54. return AVERROR(EINVAL);
  55. }
  56. avctx->frame_size = ROQ_FRAME_SIZE;
  57. avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
  58. (22050 / ROQ_FRAME_SIZE) * 8;
  59. context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
  60. sizeof(*context->frame_buffer));
  61. if (!context->frame_buffer) {
  62. ret = AVERROR(ENOMEM);
  63. goto error;
  64. }
  65. context->lastSample[0] = context->lastSample[1] = 0;
  66. return 0;
  67. error:
  68. roq_dpcm_encode_close(avctx);
  69. return ret;
  70. }
  71. static unsigned char dpcm_predict(short *previous, short current)
  72. {
  73. int diff;
  74. int negative;
  75. int result;
  76. int predicted;
  77. diff = current - *previous;
  78. negative = diff<0;
  79. diff = FFABS(diff);
  80. if (diff >= MAX_DPCM)
  81. result = 127;
  82. else {
  83. result = ff_sqrt(diff);
  84. result += diff > result*result+result;
  85. }
  86. /* See if this overflows */
  87. retry:
  88. diff = result*result;
  89. if (negative)
  90. diff = -diff;
  91. predicted = *previous + diff;
  92. /* If it overflows, back off a step */
  93. if (predicted > 32767 || predicted < -32768) {
  94. result--;
  95. goto retry;
  96. }
  97. /* Add the sign bit */
  98. result |= negative << 7; //if (negative) result |= 128;
  99. *previous = predicted;
  100. return result;
  101. }
  102. static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  103. const AVFrame *frame, int *got_packet_ptr)
  104. {
  105. int i, stereo, data_size, ret;
  106. const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
  107. uint8_t *out;
  108. ROQDPCMContext *context = avctx->priv_data;
  109. stereo = (avctx->channels == 2);
  110. if (!in && context->input_frames >= 8)
  111. return 0;
  112. if (in && context->input_frames < 8) {
  113. memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
  114. in, avctx->frame_size * avctx->channels * sizeof(*in));
  115. context->buffered_samples += avctx->frame_size;
  116. if (context->input_frames == 0)
  117. context->first_pts = frame->pts;
  118. if (context->input_frames < 7) {
  119. context->input_frames++;
  120. return 0;
  121. }
  122. in = context->frame_buffer;
  123. }
  124. if (stereo) {
  125. context->lastSample[0] &= 0xFF00;
  126. context->lastSample[1] &= 0xFF00;
  127. }
  128. if (context->input_frames == 7 || !in)
  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 = CODEC_CAP_DELAY,
  166. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  167. AV_SAMPLE_FMT_NONE },
  168. };