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.

152 lines
4.4KB

  1. /*
  2. * Interface to libgsm for GSM 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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 decoding
  25. */
  26. // The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html
  27. #include "config.h"
  28. #if HAVE_GSM_H
  29. #include <gsm.h>
  30. #else
  31. #include <gsm/gsm.h>
  32. #endif
  33. #include "libavutil/channel_layout.h"
  34. #include "libavutil/common.h"
  35. #include "avcodec.h"
  36. #include "internal.h"
  37. #include "gsm.h"
  38. typedef struct LibGSMDecodeContext {
  39. struct gsm_state *state;
  40. } LibGSMDecodeContext;
  41. static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
  42. LibGSMDecodeContext *s = avctx->priv_data;
  43. avctx->channels = 1;
  44. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  45. avctx->sample_rate = 8000;
  46. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  47. s->state = gsm_create();
  48. switch(avctx->codec_id) {
  49. case AV_CODEC_ID_GSM:
  50. avctx->frame_size = GSM_FRAME_SIZE;
  51. avctx->block_align = GSM_BLOCK_SIZE;
  52. break;
  53. case AV_CODEC_ID_GSM_MS: {
  54. int one = 1;
  55. gsm_option(s->state, GSM_OPT_WAV49, &one);
  56. avctx->frame_size = 2 * GSM_FRAME_SIZE;
  57. avctx->block_align = GSM_MS_BLOCK_SIZE;
  58. }
  59. }
  60. return 0;
  61. }
  62. static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
  63. LibGSMDecodeContext *s = avctx->priv_data;
  64. gsm_destroy(s->state);
  65. s->state = NULL;
  66. return 0;
  67. }
  68. static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
  69. int *got_frame_ptr, AVPacket *avpkt)
  70. {
  71. int i, ret;
  72. LibGSMDecodeContext *s = avctx->priv_data;
  73. AVFrame *frame = data;
  74. uint8_t *buf = avpkt->data;
  75. int buf_size = avpkt->size;
  76. int16_t *samples;
  77. if (buf_size < avctx->block_align) {
  78. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. /* get output buffer */
  82. frame->nb_samples = avctx->frame_size;
  83. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  84. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  85. return ret;
  86. }
  87. samples = (int16_t *)frame->data[0];
  88. for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
  89. if ((ret = gsm_decode(s->state, buf, samples)) < 0)
  90. return -1;
  91. buf += GSM_BLOCK_SIZE;
  92. samples += GSM_FRAME_SIZE;
  93. }
  94. *got_frame_ptr = 1;
  95. return avctx->block_align;
  96. }
  97. static void libgsm_flush(AVCodecContext *avctx) {
  98. LibGSMDecodeContext *s = avctx->priv_data;
  99. int one = 1;
  100. gsm_destroy(s->state);
  101. s->state = gsm_create();
  102. if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
  103. gsm_option(s->state, GSM_OPT_WAV49, &one);
  104. }
  105. AVCodec ff_libgsm_decoder = {
  106. .name = "libgsm",
  107. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  108. .type = AVMEDIA_TYPE_AUDIO,
  109. .id = AV_CODEC_ID_GSM,
  110. .priv_data_size = sizeof(LibGSMDecodeContext),
  111. .init = libgsm_decode_init,
  112. .close = libgsm_decode_close,
  113. .decode = libgsm_decode_frame,
  114. .flush = libgsm_flush,
  115. .capabilities = AV_CODEC_CAP_DR1,
  116. };
  117. AVCodec ff_libgsm_ms_decoder = {
  118. .name = "libgsm_ms",
  119. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  120. .type = AVMEDIA_TYPE_AUDIO,
  121. .id = AV_CODEC_ID_GSM_MS,
  122. .priv_data_size = sizeof(LibGSMDecodeContext),
  123. .init = libgsm_decode_init,
  124. .close = libgsm_decode_close,
  125. .decode = libgsm_decode_frame,
  126. .flush = libgsm_flush,
  127. .capabilities = AV_CODEC_CAP_DR1,
  128. };