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.

245 lines
7.4KB

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