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.

96 lines
2.4KB

  1. /*
  2. * Interface to libgsm for gsm encoding/decoding
  3. * Copyright (c) 2005 Alban Bedel <albeu@free.fr>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file libgsm.c
  21. * Interface to libgsm for gsm encoding/decoding
  22. */
  23. #include "avcodec.h"
  24. #include <gsm.h>
  25. // gsm.h miss some essential constants
  26. #define GSM_BLOCK_SIZE 33
  27. #define GSM_FRAME_SIZE 160
  28. static int libgsm_init(AVCodecContext *avctx) {
  29. if (avctx->channels > 1 || avctx->sample_rate != 8000)
  30. return -1;
  31. avctx->frame_size = GSM_FRAME_SIZE;
  32. avctx->block_align = GSM_BLOCK_SIZE;
  33. avctx->priv_data = gsm_create();
  34. avctx->coded_frame= avcodec_alloc_frame();
  35. avctx->coded_frame->key_frame= 1;
  36. return 0;
  37. }
  38. static int libgsm_close(AVCodecContext *avctx) {
  39. gsm_destroy(avctx->priv_data);
  40. avctx->priv_data = NULL;
  41. return 0;
  42. }
  43. static int libgsm_encode_frame(AVCodecContext *avctx,
  44. unsigned char *frame, int buf_size, void *data) {
  45. // we need a full block
  46. if(buf_size < GSM_BLOCK_SIZE) return 0;
  47. gsm_encode(avctx->priv_data,data,frame);
  48. return GSM_BLOCK_SIZE;
  49. }
  50. AVCodec libgsm_encoder = {
  51. "gsm",
  52. CODEC_TYPE_AUDIO,
  53. CODEC_ID_GSM,
  54. 0,
  55. libgsm_init,
  56. libgsm_encode_frame,
  57. libgsm_close,
  58. };
  59. static int libgsm_decode_frame(AVCodecContext *avctx,
  60. void *data, int *data_size,
  61. uint8_t *buf, int buf_size) {
  62. if(buf_size < GSM_BLOCK_SIZE) return 0;
  63. if(gsm_decode(avctx->priv_data,buf,data)) return -1;
  64. *data_size = GSM_FRAME_SIZE*2;
  65. return GSM_BLOCK_SIZE;
  66. }
  67. AVCodec libgsm_decoder = {
  68. "gsm",
  69. CODEC_TYPE_AUDIO,
  70. CODEC_ID_GSM,
  71. 0,
  72. libgsm_init,
  73. NULL,
  74. libgsm_close,
  75. libgsm_decode_frame,
  76. };