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.

121 lines
3.6KB

  1. /*
  2. * SBC parser
  3. *
  4. * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
  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. #include "sbc.h"
  23. #include "parser.h"
  24. typedef struct SBCParseContext {
  25. ParseContext pc;
  26. uint8_t header[3];
  27. int header_size;
  28. int buffered_size;
  29. } SBCParseContext;
  30. static int sbc_parse_header(AVCodecParserContext *s, AVCodecContext *avctx,
  31. const uint8_t *data, size_t len)
  32. {
  33. static const int sample_rates[4] = { 16000, 32000, 44100, 48000 };
  34. int sr, blocks, mode, subbands, bitpool, channels, joint;
  35. int length;
  36. if (len < 3)
  37. return -1;
  38. if (data[0] == MSBC_SYNCWORD && data[1] == 0 && data[2] == 0) {
  39. avctx->channels = 1;
  40. avctx->sample_rate = 16000;
  41. avctx->frame_size = 120;
  42. s->duration = avctx->frame_size;
  43. return 57;
  44. }
  45. if (data[0] != SBC_SYNCWORD)
  46. return -2;
  47. sr = (data[1] >> 6) & 0x03;
  48. blocks = (((data[1] >> 4) & 0x03) + 1) << 2;
  49. mode = (data[1] >> 2) & 0x03;
  50. subbands = (((data[1] >> 0) & 0x01) + 1) << 2;
  51. bitpool = data[2];
  52. channels = mode == SBC_MODE_MONO ? 1 : 2;
  53. joint = mode == SBC_MODE_JOINT_STEREO;
  54. length = 4 + (subbands * channels) / 2
  55. + ((((mode == SBC_MODE_DUAL_CHANNEL) + 1) * blocks * bitpool
  56. + (joint * subbands)) + 7) / 8;
  57. avctx->channels = channels;
  58. avctx->sample_rate = sample_rates[sr];
  59. avctx->frame_size = subbands * blocks;
  60. s->duration = avctx->frame_size;
  61. return length;
  62. }
  63. static int sbc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  64. const uint8_t **poutbuf, int *poutbuf_size,
  65. const uint8_t *buf, int buf_size)
  66. {
  67. SBCParseContext *pc = s->priv_data;
  68. int next;
  69. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  70. next = buf_size;
  71. } else {
  72. if (pc->header_size) {
  73. memcpy(pc->header + pc->header_size, buf,
  74. sizeof(pc->header) - pc->header_size);
  75. next = sbc_parse_header(s, avctx, pc->header, sizeof(pc->header))
  76. - pc->buffered_size;
  77. pc->header_size = 0;
  78. } else {
  79. next = sbc_parse_header(s, avctx, buf, buf_size);
  80. if (next >= buf_size)
  81. next = -1;
  82. }
  83. if (next < 0) {
  84. pc->header_size = FFMIN(sizeof(pc->header), buf_size);
  85. memcpy(pc->header, buf, pc->header_size);
  86. pc->buffered_size = buf_size;
  87. next = END_NOT_FOUND;
  88. }
  89. if (ff_combine_frame(&pc->pc, next, &buf, &buf_size) < 0) {
  90. *poutbuf = NULL;
  91. *poutbuf_size = 0;
  92. return buf_size;
  93. }
  94. }
  95. *poutbuf = buf;
  96. *poutbuf_size = buf_size;
  97. return next;
  98. }
  99. AVCodecParser ff_sbc_parser = {
  100. .codec_ids = { AV_CODEC_ID_SBC },
  101. .priv_data_size = sizeof(SBCParseContext),
  102. .parser_parse = sbc_parse,
  103. .parser_close = ff_parse_close,
  104. };