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.

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