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.

264 lines
7.9KB

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