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