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.

205 lines
6.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
  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/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_encode_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. if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
  43. return -1;
  44. }
  45. if (avctx->bit_rate != 13000 /* Official */ &&
  46. avctx->bit_rate != 13200 /* Very common */ &&
  47. avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
  48. av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
  49. avctx->bit_rate);
  50. if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
  51. return -1;
  52. }
  53. avctx->priv_data = gsm_create();
  54. switch(avctx->codec_id) {
  55. case CODEC_ID_GSM:
  56. avctx->frame_size = GSM_FRAME_SIZE;
  57. avctx->block_align = GSM_BLOCK_SIZE;
  58. break;
  59. case CODEC_ID_GSM_MS: {
  60. int one = 1;
  61. gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
  62. avctx->frame_size = 2*GSM_FRAME_SIZE;
  63. avctx->block_align = GSM_MS_BLOCK_SIZE;
  64. }
  65. }
  66. avctx->coded_frame= avcodec_alloc_frame();
  67. avctx->coded_frame->key_frame= 1;
  68. return 0;
  69. }
  70. static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
  71. av_freep(&avctx->coded_frame);
  72. gsm_destroy(avctx->priv_data);
  73. avctx->priv_data = NULL;
  74. return 0;
  75. }
  76. static int libgsm_encode_frame(AVCodecContext *avctx,
  77. unsigned char *frame, int buf_size, void *data) {
  78. // we need a full block
  79. if(buf_size < avctx->block_align) return 0;
  80. switch(avctx->codec_id) {
  81. case CODEC_ID_GSM:
  82. gsm_encode(avctx->priv_data,data,frame);
  83. break;
  84. case CODEC_ID_GSM_MS:
  85. gsm_encode(avctx->priv_data,data,frame);
  86. gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
  87. }
  88. return avctx->block_align;
  89. }
  90. AVCodec ff_libgsm_encoder = {
  91. .name = "libgsm",
  92. .type = AVMEDIA_TYPE_AUDIO,
  93. .id = CODEC_ID_GSM,
  94. .init = libgsm_encode_init,
  95. .encode = libgsm_encode_frame,
  96. .close = libgsm_encode_close,
  97. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  98. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  99. };
  100. AVCodec ff_libgsm_ms_encoder = {
  101. .name = "libgsm_ms",
  102. .type = AVMEDIA_TYPE_AUDIO,
  103. .id = CODEC_ID_GSM_MS,
  104. .init = libgsm_encode_init,
  105. .encode = libgsm_encode_frame,
  106. .close = libgsm_encode_close,
  107. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  108. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  109. };
  110. static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
  111. if (avctx->channels > 1) {
  112. av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
  113. avctx->channels);
  114. return -1;
  115. }
  116. if (!avctx->channels)
  117. avctx->channels = 1;
  118. if (!avctx->sample_rate)
  119. avctx->sample_rate = 8000;
  120. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  121. avctx->priv_data = gsm_create();
  122. switch(avctx->codec_id) {
  123. case CODEC_ID_GSM:
  124. avctx->frame_size = GSM_FRAME_SIZE;
  125. avctx->block_align = GSM_BLOCK_SIZE;
  126. break;
  127. case CODEC_ID_GSM_MS: {
  128. int one = 1;
  129. gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
  130. avctx->frame_size = 2 * GSM_FRAME_SIZE;
  131. avctx->block_align = GSM_MS_BLOCK_SIZE;
  132. }
  133. }
  134. return 0;
  135. }
  136. static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
  137. gsm_destroy(avctx->priv_data);
  138. avctx->priv_data = NULL;
  139. return 0;
  140. }
  141. static int libgsm_decode_frame(AVCodecContext *avctx,
  142. void *data, int *data_size,
  143. AVPacket *avpkt) {
  144. uint8_t *buf = avpkt->data;
  145. int buf_size = avpkt->size;
  146. *data_size = 0; /* In case of error */
  147. if(buf_size < avctx->block_align) return -1;
  148. switch(avctx->codec_id) {
  149. case CODEC_ID_GSM:
  150. if(gsm_decode(avctx->priv_data,buf,data)) return -1;
  151. *data_size = GSM_FRAME_SIZE*sizeof(int16_t);
  152. break;
  153. case CODEC_ID_GSM_MS:
  154. if(gsm_decode(avctx->priv_data,buf,data) ||
  155. gsm_decode(avctx->priv_data,buf+33,((int16_t*)data)+GSM_FRAME_SIZE)) return -1;
  156. *data_size = GSM_FRAME_SIZE*sizeof(int16_t)*2;
  157. }
  158. return avctx->block_align;
  159. }
  160. AVCodec ff_libgsm_decoder = {
  161. .name = "libgsm",
  162. .type = AVMEDIA_TYPE_AUDIO,
  163. .id = CODEC_ID_GSM,
  164. .init = libgsm_decode_init,
  165. .close = libgsm_decode_close,
  166. .decode = libgsm_decode_frame,
  167. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  168. };
  169. AVCodec ff_libgsm_ms_decoder = {
  170. .name = "libgsm_ms",
  171. .type = AVMEDIA_TYPE_AUDIO,
  172. .id = CODEC_ID_GSM_MS,
  173. .init = libgsm_decode_init,
  174. .close = libgsm_decode_close,
  175. .decode = libgsm_decode_frame,
  176. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  177. };