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.

151 lines
4.2KB

  1. /*
  2. * MPEG Audio header decoder
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * MPEG Audio header decoder.
  24. */
  25. #include "libavutil/common.h"
  26. #include "avcodec.h"
  27. #include "mpegaudio.h"
  28. #include "mpegaudiodata.h"
  29. #include "mpegaudiodecheader.h"
  30. int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
  31. {
  32. int sample_rate, frame_size, mpeg25, padding;
  33. int sample_rate_index, bitrate_index;
  34. if (header & (1<<20)) {
  35. s->lsf = (header & (1<<19)) ? 0 : 1;
  36. mpeg25 = 0;
  37. } else {
  38. s->lsf = 1;
  39. mpeg25 = 1;
  40. }
  41. s->layer = 4 - ((header >> 17) & 3);
  42. /* extract frequency */
  43. sample_rate_index = (header >> 10) & 3;
  44. if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
  45. sample_rate_index = 0;
  46. sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
  47. sample_rate_index += 3 * (s->lsf + mpeg25);
  48. s->sample_rate_index = sample_rate_index;
  49. s->error_protection = ((header >> 16) & 1) ^ 1;
  50. s->sample_rate = sample_rate;
  51. bitrate_index = (header >> 12) & 0xf;
  52. padding = (header >> 9) & 1;
  53. //extension = (header >> 8) & 1;
  54. s->mode = (header >> 6) & 3;
  55. s->mode_ext = (header >> 4) & 3;
  56. //copyright = (header >> 3) & 1;
  57. //original = (header >> 2) & 1;
  58. //emphasis = header & 3;
  59. if (s->mode == MPA_MONO)
  60. s->nb_channels = 1;
  61. else
  62. s->nb_channels = 2;
  63. if (bitrate_index != 0) {
  64. frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
  65. s->bit_rate = frame_size * 1000;
  66. switch(s->layer) {
  67. case 1:
  68. frame_size = (frame_size * 12000) / sample_rate;
  69. frame_size = (frame_size + padding) * 4;
  70. break;
  71. case 2:
  72. frame_size = (frame_size * 144000) / sample_rate;
  73. frame_size += padding;
  74. break;
  75. default:
  76. case 3:
  77. frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
  78. frame_size += padding;
  79. break;
  80. }
  81. s->frame_size = frame_size;
  82. } else {
  83. /* if no frame size computed, signal it */
  84. return 1;
  85. }
  86. #if defined(DEBUG)
  87. ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
  88. s->layer, s->sample_rate, s->bit_rate);
  89. if (s->nb_channels == 2) {
  90. if (s->layer == 3) {
  91. if (s->mode_ext & MODE_EXT_MS_STEREO)
  92. ff_dlog(NULL, "ms-");
  93. if (s->mode_ext & MODE_EXT_I_STEREO)
  94. ff_dlog(NULL, "i-");
  95. }
  96. ff_dlog(NULL, "stereo");
  97. } else {
  98. ff_dlog(NULL, "mono");
  99. }
  100. ff_dlog(NULL, "\n");
  101. #endif
  102. return 0;
  103. }
  104. int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
  105. {
  106. MPADecodeHeader s1, *s = &s1;
  107. if (ff_mpa_check_header(head) != 0)
  108. return -1;
  109. if (avpriv_mpegaudio_decode_header(s, head) != 0) {
  110. return -1;
  111. }
  112. switch(s->layer) {
  113. case 1:
  114. avctx->codec_id = AV_CODEC_ID_MP1;
  115. *frame_size = 384;
  116. break;
  117. case 2:
  118. avctx->codec_id = AV_CODEC_ID_MP2;
  119. *frame_size = 1152;
  120. break;
  121. default:
  122. case 3:
  123. if (avctx->codec_id != AV_CODEC_ID_MP3ADU)
  124. avctx->codec_id = AV_CODEC_ID_MP3;
  125. if (s->lsf)
  126. *frame_size = 576;
  127. else
  128. *frame_size = 1152;
  129. break;
  130. }
  131. *sample_rate = s->sample_rate;
  132. *channels = s->nb_channels;
  133. *bit_rate = s->bit_rate;
  134. return s->frame_size;
  135. }