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.

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