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.

247 lines
7.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 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 encoding/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. static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
  39. if (avctx->channels > 1) {
  40. av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
  41. avctx->channels);
  42. return -1;
  43. }
  44. if (avctx->sample_rate != 8000) {
  45. av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
  46. avctx->sample_rate);
  47. if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
  48. return -1;
  49. }
  50. if (avctx->bit_rate != 13000 /* Official */ &&
  51. avctx->bit_rate != 13200 /* Very common */ &&
  52. avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
  53. av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
  54. avctx->bit_rate);
  55. if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
  56. return -1;
  57. }
  58. avctx->priv_data = gsm_create();
  59. switch(avctx->codec_id) {
  60. case AV_CODEC_ID_GSM:
  61. avctx->frame_size = GSM_FRAME_SIZE;
  62. avctx->block_align = GSM_BLOCK_SIZE;
  63. break;
  64. case AV_CODEC_ID_GSM_MS: {
  65. int one = 1;
  66. gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
  67. avctx->frame_size = 2*GSM_FRAME_SIZE;
  68. avctx->block_align = GSM_MS_BLOCK_SIZE;
  69. }
  70. }
  71. return 0;
  72. }
  73. static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
  74. gsm_destroy(avctx->priv_data);
  75. avctx->priv_data = NULL;
  76. return 0;
  77. }
  78. static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  79. const AVFrame *frame, int *got_packet_ptr)
  80. {
  81. int ret;
  82. gsm_signal *samples = (gsm_signal *)frame->data[0];
  83. struct gsm_state *state = avctx->priv_data;
  84. if ((ret = ff_alloc_packet(avpkt, avctx->block_align))) {
  85. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  86. return ret;
  87. }
  88. switch(avctx->codec_id) {
  89. case AV_CODEC_ID_GSM:
  90. gsm_encode(state, samples, avpkt->data);
  91. break;
  92. case AV_CODEC_ID_GSM_MS:
  93. gsm_encode(state, samples, avpkt->data);
  94. gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
  95. }
  96. *got_packet_ptr = 1;
  97. return 0;
  98. }
  99. AVCodec ff_libgsm_encoder = {
  100. .name = "libgsm",
  101. .type = AVMEDIA_TYPE_AUDIO,
  102. .id = AV_CODEC_ID_GSM,
  103. .init = libgsm_encode_init,
  104. .encode2 = libgsm_encode_frame,
  105. .close = libgsm_encode_close,
  106. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  107. AV_SAMPLE_FMT_NONE },
  108. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  109. };
  110. AVCodec ff_libgsm_ms_encoder = {
  111. .name = "libgsm_ms",
  112. .type = AVMEDIA_TYPE_AUDIO,
  113. .id = AV_CODEC_ID_GSM_MS,
  114. .init = libgsm_encode_init,
  115. .encode2 = libgsm_encode_frame,
  116. .close = libgsm_encode_close,
  117. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  118. AV_SAMPLE_FMT_NONE },
  119. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  120. };
  121. typedef struct LibGSMDecodeContext {
  122. struct gsm_state *state;
  123. } LibGSMDecodeContext;
  124. static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
  125. LibGSMDecodeContext *s = avctx->priv_data;
  126. avctx->channels = 1;
  127. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  128. avctx->sample_rate = 8000;
  129. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  130. s->state = gsm_create();
  131. switch(avctx->codec_id) {
  132. case AV_CODEC_ID_GSM:
  133. avctx->frame_size = GSM_FRAME_SIZE;
  134. avctx->block_align = GSM_BLOCK_SIZE;
  135. break;
  136. case AV_CODEC_ID_GSM_MS: {
  137. int one = 1;
  138. gsm_option(s->state, GSM_OPT_WAV49, &one);
  139. avctx->frame_size = 2 * GSM_FRAME_SIZE;
  140. avctx->block_align = GSM_MS_BLOCK_SIZE;
  141. }
  142. }
  143. return 0;
  144. }
  145. static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
  146. LibGSMDecodeContext *s = avctx->priv_data;
  147. gsm_destroy(s->state);
  148. s->state = NULL;
  149. return 0;
  150. }
  151. static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
  152. int *got_frame_ptr, AVPacket *avpkt)
  153. {
  154. int i, ret;
  155. LibGSMDecodeContext *s = avctx->priv_data;
  156. AVFrame *frame = data;
  157. uint8_t *buf = avpkt->data;
  158. int buf_size = avpkt->size;
  159. int16_t *samples;
  160. if (buf_size < avctx->block_align) {
  161. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  162. return AVERROR_INVALIDDATA;
  163. }
  164. /* get output buffer */
  165. frame->nb_samples = avctx->frame_size;
  166. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  167. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  168. return ret;
  169. }
  170. samples = (int16_t *)frame->data[0];
  171. for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
  172. if ((ret = gsm_decode(s->state, buf, samples)) < 0)
  173. return -1;
  174. buf += GSM_BLOCK_SIZE;
  175. samples += GSM_FRAME_SIZE;
  176. }
  177. *got_frame_ptr = 1;
  178. return avctx->block_align;
  179. }
  180. static void libgsm_flush(AVCodecContext *avctx) {
  181. LibGSMDecodeContext *s = avctx->priv_data;
  182. int one = 1;
  183. gsm_destroy(s->state);
  184. s->state = gsm_create();
  185. if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
  186. gsm_option(s->state, GSM_OPT_WAV49, &one);
  187. }
  188. AVCodec ff_libgsm_decoder = {
  189. .name = "libgsm",
  190. .type = AVMEDIA_TYPE_AUDIO,
  191. .id = AV_CODEC_ID_GSM,
  192. .priv_data_size = sizeof(LibGSMDecodeContext),
  193. .init = libgsm_decode_init,
  194. .close = libgsm_decode_close,
  195. .decode = libgsm_decode_frame,
  196. .flush = libgsm_flush,
  197. .capabilities = CODEC_CAP_DR1,
  198. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  199. };
  200. AVCodec ff_libgsm_ms_decoder = {
  201. .name = "libgsm_ms",
  202. .type = AVMEDIA_TYPE_AUDIO,
  203. .id = AV_CODEC_ID_GSM_MS,
  204. .priv_data_size = sizeof(LibGSMDecodeContext),
  205. .init = libgsm_decode_init,
  206. .close = libgsm_decode_close,
  207. .decode = libgsm_decode_frame,
  208. .flush = libgsm_flush,
  209. .capabilities = CODEC_CAP_DR1,
  210. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  211. };