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.

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