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.

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