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.

266 lines
7.9KB

  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)) < 0)
  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. struct gsm_state *state;
  138. } LibGSMDecodeContext;
  139. static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
  140. LibGSMDecodeContext *s = avctx->priv_data;
  141. avctx->channels = 1;
  142. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  143. if (!avctx->sample_rate)
  144. avctx->sample_rate = 8000;
  145. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  146. s->state = gsm_create();
  147. switch(avctx->codec_id) {
  148. case AV_CODEC_ID_GSM:
  149. avctx->frame_size = GSM_FRAME_SIZE;
  150. avctx->block_align = GSM_BLOCK_SIZE;
  151. break;
  152. case AV_CODEC_ID_GSM_MS: {
  153. int one = 1;
  154. gsm_option(s->state, GSM_OPT_WAV49, &one);
  155. avctx->frame_size = 2 * GSM_FRAME_SIZE;
  156. avctx->block_align = GSM_MS_BLOCK_SIZE;
  157. }
  158. }
  159. return 0;
  160. }
  161. static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
  162. LibGSMDecodeContext *s = avctx->priv_data;
  163. gsm_destroy(s->state);
  164. s->state = NULL;
  165. return 0;
  166. }
  167. static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
  168. int *got_frame_ptr, AVPacket *avpkt)
  169. {
  170. int i, ret;
  171. LibGSMDecodeContext *s = avctx->priv_data;
  172. AVFrame *frame = data;
  173. uint8_t *buf = avpkt->data;
  174. int buf_size = avpkt->size;
  175. int16_t *samples;
  176. if (buf_size < avctx->block_align) {
  177. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  178. return AVERROR_INVALIDDATA;
  179. }
  180. /* get output buffer */
  181. frame->nb_samples = avctx->frame_size;
  182. if ((ret = ff_get_buffer(avctx, frame)) < 0) {
  183. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  184. return ret;
  185. }
  186. samples = (int16_t *)frame->data[0];
  187. for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
  188. if ((ret = gsm_decode(s->state, buf, samples)) < 0)
  189. return -1;
  190. buf += GSM_BLOCK_SIZE;
  191. samples += GSM_FRAME_SIZE;
  192. }
  193. *got_frame_ptr = 1;
  194. return avctx->block_align;
  195. }
  196. static void libgsm_flush(AVCodecContext *avctx) {
  197. LibGSMDecodeContext *s = avctx->priv_data;
  198. int one = 1;
  199. gsm_destroy(s->state);
  200. s->state = gsm_create();
  201. if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
  202. gsm_option(s->state, GSM_OPT_WAV49, &one);
  203. }
  204. #if CONFIG_LIBGSM_DECODER
  205. AVCodec ff_libgsm_decoder = {
  206. .name = "libgsm",
  207. .type = AVMEDIA_TYPE_AUDIO,
  208. .id = AV_CODEC_ID_GSM,
  209. .priv_data_size = sizeof(LibGSMDecodeContext),
  210. .init = libgsm_decode_init,
  211. .close = libgsm_decode_close,
  212. .decode = libgsm_decode_frame,
  213. .flush = libgsm_flush,
  214. .capabilities = CODEC_CAP_DR1,
  215. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
  216. };
  217. #endif
  218. #if CONFIG_LIBGSM_MS_DECODER
  219. AVCodec ff_libgsm_ms_decoder = {
  220. .name = "libgsm_ms",
  221. .type = AVMEDIA_TYPE_AUDIO,
  222. .id = AV_CODEC_ID_GSM_MS,
  223. .priv_data_size = sizeof(LibGSMDecodeContext),
  224. .init = libgsm_decode_init,
  225. .close = libgsm_decode_close,
  226. .decode = libgsm_decode_frame,
  227. .flush = libgsm_flush,
  228. .capabilities = CODEC_CAP_DR1,
  229. .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
  230. };
  231. #endif