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.

270 lines
8.0KB

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