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.

146 lines
3.8KB

  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 miss some essential constants
  30. #define GSM_BLOCK_SIZE 33
  31. #define GSM_MS_BLOCK_SIZE 65
  32. #define GSM_FRAME_SIZE 160
  33. static int libgsm_init(AVCodecContext *avctx) {
  34. if (avctx->channels > 1 || avctx->sample_rate != 8000 || avctx->bit_rate != 13000)
  35. return -1;
  36. avctx->priv_data = gsm_create();
  37. switch(avctx->codec_id) {
  38. case CODEC_ID_GSM:
  39. avctx->frame_size = GSM_FRAME_SIZE;
  40. avctx->block_align = GSM_BLOCK_SIZE;
  41. break;
  42. case CODEC_ID_GSM_MS: {
  43. int one = 1;
  44. gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
  45. avctx->frame_size = 2*GSM_FRAME_SIZE;
  46. avctx->block_align = GSM_MS_BLOCK_SIZE;
  47. }
  48. }
  49. avctx->coded_frame= avcodec_alloc_frame();
  50. avctx->coded_frame->key_frame= 1;
  51. return 0;
  52. }
  53. static int libgsm_close(AVCodecContext *avctx) {
  54. gsm_destroy(avctx->priv_data);
  55. avctx->priv_data = NULL;
  56. return 0;
  57. }
  58. static int libgsm_encode_frame(AVCodecContext *avctx,
  59. unsigned char *frame, int buf_size, void *data) {
  60. // we need a full block
  61. if(buf_size < avctx->block_align) return 0;
  62. switch(avctx->codec_id) {
  63. case CODEC_ID_GSM:
  64. gsm_encode(avctx->priv_data,data,frame);
  65. break;
  66. case CODEC_ID_GSM_MS:
  67. gsm_encode(avctx->priv_data,data,frame);
  68. gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
  69. }
  70. return avctx->block_align;
  71. }
  72. AVCodec libgsm_encoder = {
  73. "gsm",
  74. CODEC_TYPE_AUDIO,
  75. CODEC_ID_GSM,
  76. 0,
  77. libgsm_init,
  78. libgsm_encode_frame,
  79. libgsm_close,
  80. };
  81. AVCodec libgsm_ms_encoder = {
  82. "gsm",
  83. CODEC_TYPE_AUDIO,
  84. CODEC_ID_GSM_MS,
  85. 0,
  86. libgsm_init,
  87. libgsm_encode_frame,
  88. libgsm_close,
  89. };
  90. static int libgsm_decode_frame(AVCodecContext *avctx,
  91. void *data, int *data_size,
  92. uint8_t *buf, int buf_size) {
  93. if(buf_size < avctx->block_align) return 0;
  94. switch(avctx->codec_id) {
  95. case CODEC_ID_GSM:
  96. if(gsm_decode(avctx->priv_data,buf,data)) return -1;
  97. *data_size = GSM_FRAME_SIZE*sizeof(int16_t);
  98. break;
  99. case CODEC_ID_GSM_MS:
  100. if(gsm_decode(avctx->priv_data,buf,data) ||
  101. gsm_decode(avctx->priv_data,buf+33,((int16_t*)data)+GSM_FRAME_SIZE)) return -1;
  102. *data_size = GSM_FRAME_SIZE*sizeof(int16_t)*2;
  103. }
  104. return avctx->block_align;
  105. }
  106. AVCodec libgsm_decoder = {
  107. "gsm",
  108. CODEC_TYPE_AUDIO,
  109. CODEC_ID_GSM,
  110. 0,
  111. libgsm_init,
  112. NULL,
  113. libgsm_close,
  114. libgsm_decode_frame,
  115. };
  116. AVCodec libgsm_ms_decoder = {
  117. "gsm_ms",
  118. CODEC_TYPE_AUDIO,
  119. CODEC_ID_GSM_MS,
  120. 0,
  121. libgsm_init,
  122. NULL,
  123. libgsm_close,
  124. libgsm_decode_frame,
  125. };