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.

272 lines
8.1KB

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