mpeg4audio.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * MPEG-4 Audio common code
  3. * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "bitstream.h"
  22. #include "mpeg4audio.h"
  23. const int ff_mpeg4audio_sample_rates[16] = {
  24. 96000, 88200, 64000, 48000, 44100, 32000,
  25. 24000, 22050, 16000, 12000, 11025, 8000, 7350
  26. };
  27. const uint8_t ff_mpeg4audio_channels[8] = {
  28. 0, 1, 2, 3, 4, 5, 6, 8
  29. };
  30. static inline int get_object_type(GetBitContext *gb)
  31. {
  32. int object_type = get_bits(gb, 5);
  33. if (object_type == 31)
  34. object_type = 32 + get_bits(gb, 6);
  35. return object_type;
  36. }
  37. static inline int get_sample_rate(GetBitContext *gb, int *index)
  38. {
  39. *index = get_bits(gb, 4);
  40. return *index == 0x0f ? get_bits(gb, 24) :
  41. ff_mpeg4audio_sample_rates[*index];
  42. }
  43. int ff_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int buf_size)
  44. {
  45. GetBitContext gb;
  46. int specific_config_bitindex;
  47. init_get_bits(&gb, buf, buf_size*8);
  48. c->object_type = get_object_type(&gb);
  49. c->sample_rate = get_sample_rate(&gb, &c->sampling_index);
  50. c->chan_config = get_bits(&gb, 4);
  51. c->sbr = -1;
  52. if (c->object_type == 5) {
  53. c->ext_object_type = c->object_type;
  54. c->sbr = 1;
  55. c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index);
  56. c->object_type = get_object_type(&gb);
  57. } else
  58. c->ext_object_type = 0;
  59. specific_config_bitindex = get_bits_count(&gb);
  60. if (c->ext_object_type != 5) {
  61. int bits_left = buf_size*8 - specific_config_bitindex;
  62. for (; bits_left > 15; bits_left--) {
  63. if (show_bits(&gb, 11) == 0x2b7) { // sync extension
  64. get_bits(&gb, 11);
  65. c->ext_object_type = get_object_type(&gb);
  66. if (c->ext_object_type == 5 && (c->sbr = get_bits1(&gb)) == 1)
  67. c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index);
  68. break;
  69. } else
  70. get_bits1(&gb); // skip 1 bit
  71. }
  72. }
  73. return specific_config_bitindex;
  74. }