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.

251 lines
7.5KB

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