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.

218 lines
6.0KB

  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. #if FF_API_OLD_ENCODE_AUDIO
  42. av_freep(&avctx->coded_frame);
  43. #endif
  44. av_freep(&context->frame_buffer);
  45. return 0;
  46. }
  47. static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
  48. {
  49. ROQDPCMContext *context = avctx->priv_data;
  50. int ret;
  51. if (avctx->channels > 2) {
  52. av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
  53. return AVERROR(EINVAL);
  54. }
  55. if (avctx->sample_rate != 22050) {
  56. av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
  57. return AVERROR(EINVAL);
  58. }
  59. avctx->frame_size = ROQ_FRAME_SIZE;
  60. avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
  61. (22050 / ROQ_FRAME_SIZE) * 8;
  62. context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
  63. sizeof(*context->frame_buffer));
  64. if (!context->frame_buffer) {
  65. ret = AVERROR(ENOMEM);
  66. goto error;
  67. }
  68. context->lastSample[0] = context->lastSample[1] = 0;
  69. #if FF_API_OLD_ENCODE_AUDIO
  70. avctx->coded_frame= avcodec_alloc_frame();
  71. if (!avctx->coded_frame) {
  72. ret = AVERROR(ENOMEM);
  73. goto error;
  74. }
  75. #endif
  76. return 0;
  77. error:
  78. roq_dpcm_encode_close(avctx);
  79. return ret;
  80. }
  81. static unsigned char dpcm_predict(short *previous, short current)
  82. {
  83. int diff;
  84. int negative;
  85. int result;
  86. int predicted;
  87. diff = current - *previous;
  88. negative = diff<0;
  89. diff = FFABS(diff);
  90. if (diff >= MAX_DPCM)
  91. result = 127;
  92. else {
  93. result = ff_sqrt(diff);
  94. result += diff > result*result+result;
  95. }
  96. /* See if this overflows */
  97. retry:
  98. diff = result*result;
  99. if (negative)
  100. diff = -diff;
  101. predicted = *previous + diff;
  102. /* If it overflows, back off a step */
  103. if (predicted > 32767 || predicted < -32768) {
  104. result--;
  105. goto retry;
  106. }
  107. /* Add the sign bit */
  108. result |= negative << 7; //if (negative) result |= 128;
  109. *previous = predicted;
  110. return result;
  111. }
  112. static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  113. const AVFrame *frame, int *got_packet_ptr)
  114. {
  115. int i, stereo, data_size, ret;
  116. const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
  117. uint8_t *out;
  118. ROQDPCMContext *context = avctx->priv_data;
  119. stereo = (avctx->channels == 2);
  120. if (!in && context->input_frames >= 8)
  121. return 0;
  122. if (in && context->input_frames < 8) {
  123. memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
  124. in, avctx->frame_size * avctx->channels * sizeof(*in));
  125. context->buffered_samples += avctx->frame_size;
  126. if (context->input_frames == 0)
  127. context->first_pts = frame->pts;
  128. if (context->input_frames < 7) {
  129. context->input_frames++;
  130. return 0;
  131. }
  132. in = context->frame_buffer;
  133. }
  134. if (stereo) {
  135. context->lastSample[0] &= 0xFF00;
  136. context->lastSample[1] &= 0xFF00;
  137. }
  138. if (context->input_frames == 7 || !in)
  139. data_size = avctx->channels * context->buffered_samples;
  140. else
  141. data_size = avctx->channels * avctx->frame_size;
  142. if ((ret = ff_alloc_packet(avpkt, ROQ_HEADER_SIZE + data_size))) {
  143. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  144. return ret;
  145. }
  146. out = avpkt->data;
  147. bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
  148. bytestream_put_byte(&out, 0x10);
  149. bytestream_put_le32(&out, data_size);
  150. if (stereo) {
  151. bytestream_put_byte(&out, (context->lastSample[1])>>8);
  152. bytestream_put_byte(&out, (context->lastSample[0])>>8);
  153. } else
  154. bytestream_put_le16(&out, context->lastSample[0]);
  155. /* Write the actual samples */
  156. for (i = 0; i < data_size; i++)
  157. *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
  158. avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
  159. avpkt->duration = data_size / avctx->channels;
  160. context->input_frames++;
  161. if (!in)
  162. context->input_frames = FFMAX(context->input_frames, 8);
  163. *got_packet_ptr = 1;
  164. return 0;
  165. }
  166. AVCodec ff_roq_dpcm_encoder = {
  167. .name = "roq_dpcm",
  168. .type = AVMEDIA_TYPE_AUDIO,
  169. .id = AV_CODEC_ID_ROQ_DPCM,
  170. .priv_data_size = sizeof(ROQDPCMContext),
  171. .init = roq_dpcm_encode_init,
  172. .encode2 = roq_dpcm_encode_frame,
  173. .close = roq_dpcm_encode_close,
  174. .capabilities = CODEC_CAP_DELAY,
  175. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  176. AV_SAMPLE_FMT_NONE },
  177. .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
  178. };