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.

257 lines
7.7KB

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