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.

175 lines
5.1KB

  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->codec->decode){
  40. if(!avctx->channels)
  41. avctx->channels= 1;
  42. if(!avctx->sample_rate)
  43. avctx->sample_rate= 8000;
  44. }else{
  45. if (avctx->sample_rate != 8000) {
  46. av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
  47. avctx->sample_rate);
  48. if(avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL)
  49. return -1;
  50. }
  51. if (avctx->bit_rate != 13000 /* Official */ &&
  52. avctx->bit_rate != 13200 /* Very common */ &&
  53. avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
  54. av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
  55. avctx->bit_rate);
  56. if(avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL)
  57. return -1;
  58. }
  59. }
  60. avctx->priv_data = gsm_create();
  61. switch(avctx->codec_id) {
  62. case CODEC_ID_GSM:
  63. avctx->frame_size = GSM_FRAME_SIZE;
  64. avctx->block_align = GSM_BLOCK_SIZE;
  65. break;
  66. case CODEC_ID_GSM_MS: {
  67. int one = 1;
  68. gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
  69. avctx->frame_size = 2*GSM_FRAME_SIZE;
  70. avctx->block_align = GSM_MS_BLOCK_SIZE;
  71. }
  72. }
  73. avctx->coded_frame= avcodec_alloc_frame();
  74. avctx->coded_frame->key_frame= 1;
  75. return 0;
  76. }
  77. static av_cold int libgsm_close(AVCodecContext *avctx) {
  78. gsm_destroy(avctx->priv_data);
  79. avctx->priv_data = NULL;
  80. return 0;
  81. }
  82. static int libgsm_encode_frame(AVCodecContext *avctx,
  83. unsigned char *frame, int buf_size, void *data) {
  84. // we need a full block
  85. if(buf_size < avctx->block_align) return 0;
  86. switch(avctx->codec_id) {
  87. case CODEC_ID_GSM:
  88. gsm_encode(avctx->priv_data,data,frame);
  89. break;
  90. case CODEC_ID_GSM_MS:
  91. gsm_encode(avctx->priv_data,data,frame);
  92. gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
  93. }
  94. return avctx->block_align;
  95. }
  96. AVCodec libgsm_encoder = {
  97. "libgsm",
  98. CODEC_TYPE_AUDIO,
  99. CODEC_ID_GSM,
  100. 0,
  101. libgsm_init,
  102. libgsm_encode_frame,
  103. libgsm_close,
  104. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  105. };
  106. AVCodec libgsm_ms_encoder = {
  107. "libgsm_ms",
  108. CODEC_TYPE_AUDIO,
  109. CODEC_ID_GSM_MS,
  110. 0,
  111. libgsm_init,
  112. libgsm_encode_frame,
  113. libgsm_close,
  114. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  115. };
  116. static int libgsm_decode_frame(AVCodecContext *avctx,
  117. void *data, int *data_size,
  118. uint8_t *buf, int buf_size) {
  119. *data_size = 0; /* In case of error */
  120. if(buf_size < avctx->block_align) return -1;
  121. switch(avctx->codec_id) {
  122. case CODEC_ID_GSM:
  123. if(gsm_decode(avctx->priv_data,buf,data)) return -1;
  124. *data_size = GSM_FRAME_SIZE*sizeof(int16_t);
  125. break;
  126. case CODEC_ID_GSM_MS:
  127. if(gsm_decode(avctx->priv_data,buf,data) ||
  128. gsm_decode(avctx->priv_data,buf+33,((int16_t*)data)+GSM_FRAME_SIZE)) return -1;
  129. *data_size = GSM_FRAME_SIZE*sizeof(int16_t)*2;
  130. }
  131. return avctx->block_align;
  132. }
  133. AVCodec libgsm_decoder = {
  134. "libgsm",
  135. CODEC_TYPE_AUDIO,
  136. CODEC_ID_GSM,
  137. 0,
  138. libgsm_init,
  139. NULL,
  140. libgsm_close,
  141. libgsm_decode_frame,
  142. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  143. };
  144. AVCodec libgsm_ms_decoder = {
  145. "libgsm_ms",
  146. CODEC_TYPE_AUDIO,
  147. CODEC_ID_GSM_MS,
  148. 0,
  149. libgsm_init,
  150. NULL,
  151. libgsm_close,
  152. libgsm_decode_frame,
  153. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  154. };