libgsm.c 5.3 KB

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