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.

160 lines
4.5KB

  1. /*
  2. * Interface to libgsm for gsm encoding/decoding
  3. * Copyright (c) 2005 Alban Bedel <albeu@free.fr>
  4. * Copyright (c) 2006, 2007 Michel Bardiaux <mbardiaux@mediaxim.be>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libgsm.c
  24. * Interface to libgsm for gsm encoding/decoding
  25. */
  26. // The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html
  27. #include "avcodec.h"
  28. #include <gsm.h>
  29. // gsm.h misses some essential constants
  30. #define GSM_BLOCK_SIZE 33
  31. #define GSM_MS_BLOCK_SIZE 65
  32. #define GSM_FRAME_SIZE 160
  33. static av_cold int libgsm_init(AVCodecContext *avctx) {
  34. if (avctx->channels > 1) {
  35. av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
  36. avctx->channels);
  37. return -1;
  38. }
  39. if (avctx->sample_rate != 8000) {
  40. av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
  41. avctx->sample_rate);
  42. return -1;
  43. }
  44. if (avctx->bit_rate != 13000 /* Official */ &&
  45. avctx->bit_rate != 13200 /* Very common */ &&
  46. avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
  47. av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
  48. avctx->bit_rate);
  49. return -1;
  50. }
  51. avctx->priv_data = gsm_create();
  52. switch(avctx->codec_id) {
  53. case CODEC_ID_GSM:
  54. avctx->frame_size = GSM_FRAME_SIZE;
  55. avctx->block_align = GSM_BLOCK_SIZE;
  56. break;
  57. case CODEC_ID_GSM_MS: {
  58. int one = 1;
  59. gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
  60. avctx->frame_size = 2*GSM_FRAME_SIZE;
  61. avctx->block_align = GSM_MS_BLOCK_SIZE;
  62. }
  63. }
  64. avctx->coded_frame= avcodec_alloc_frame();
  65. avctx->coded_frame->key_frame= 1;
  66. return 0;
  67. }
  68. static av_cold int libgsm_close(AVCodecContext *avctx) {
  69. gsm_destroy(avctx->priv_data);
  70. avctx->priv_data = NULL;
  71. return 0;
  72. }
  73. static int libgsm_encode_frame(AVCodecContext *avctx,
  74. unsigned char *frame, int buf_size, void *data) {
  75. // we need a full block
  76. if(buf_size < avctx->block_align) return 0;
  77. switch(avctx->codec_id) {
  78. case CODEC_ID_GSM:
  79. gsm_encode(avctx->priv_data,data,frame);
  80. break;
  81. case CODEC_ID_GSM_MS:
  82. gsm_encode(avctx->priv_data,data,frame);
  83. gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
  84. }
  85. return avctx->block_align;
  86. }
  87. AVCodec libgsm_encoder = {
  88. "libgsm",
  89. CODEC_TYPE_AUDIO,
  90. CODEC_ID_GSM,
  91. 0,
  92. libgsm_init,
  93. libgsm_encode_frame,
  94. libgsm_close,
  95. };
  96. AVCodec libgsm_ms_encoder = {
  97. "libgsm_ms",
  98. CODEC_TYPE_AUDIO,
  99. CODEC_ID_GSM_MS,
  100. 0,
  101. libgsm_init,
  102. libgsm_encode_frame,
  103. libgsm_close,
  104. };
  105. static int libgsm_decode_frame(AVCodecContext *avctx,
  106. void *data, int *data_size,
  107. uint8_t *buf, int buf_size) {
  108. *data_size = 0; /* In case of error */
  109. if(buf_size < avctx->block_align) return -1;
  110. switch(avctx->codec_id) {
  111. case CODEC_ID_GSM:
  112. if(gsm_decode(avctx->priv_data,buf,data)) return -1;
  113. *data_size = GSM_FRAME_SIZE*sizeof(int16_t);
  114. break;
  115. case CODEC_ID_GSM_MS:
  116. if(gsm_decode(avctx->priv_data,buf,data) ||
  117. gsm_decode(avctx->priv_data,buf+33,((int16_t*)data)+GSM_FRAME_SIZE)) return -1;
  118. *data_size = GSM_FRAME_SIZE*sizeof(int16_t)*2;
  119. }
  120. return avctx->block_align;
  121. }
  122. AVCodec libgsm_decoder = {
  123. "libgsm",
  124. CODEC_TYPE_AUDIO,
  125. CODEC_ID_GSM,
  126. 0,
  127. libgsm_init,
  128. NULL,
  129. libgsm_close,
  130. libgsm_decode_frame,
  131. };
  132. AVCodec libgsm_ms_decoder = {
  133. "libgsm_ms",
  134. CODEC_TYPE_AUDIO,
  135. CODEC_ID_GSM_MS,
  136. 0,
  137. libgsm_init,
  138. NULL,
  139. libgsm_close,
  140. libgsm_decode_frame,
  141. };