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.

258 lines
7.8KB

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