mpegaudiodecheader.c 4.3 KB

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