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.

152 lines
4.5KB

  1. /*
  2. * FLAC common code
  3. * Copyright (c) 2009 Justin Ruggles
  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 "libavutil/crc.h"
  22. #include "flac.h"
  23. #include "flacdata.h"
  24. static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
  25. static int64_t get_utf8(GetBitContext *gb)
  26. {
  27. int64_t val;
  28. GET_UTF8(val, get_bits(gb, 8), return -1;)
  29. return val;
  30. }
  31. int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
  32. FLACFrameInfo *fi, int log_level_offset)
  33. {
  34. int bs_code, sr_code, bps_code;
  35. /* frame sync code */
  36. if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
  37. av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
  38. return -1;
  39. }
  40. /* variable block size stream code */
  41. fi->is_var_size = get_bits1(gb);
  42. /* block size and sample rate codes */
  43. bs_code = get_bits(gb, 4);
  44. sr_code = get_bits(gb, 4);
  45. /* channels and decorrelation */
  46. fi->ch_mode = get_bits(gb, 4);
  47. if (fi->ch_mode < FLAC_MAX_CHANNELS) {
  48. fi->channels = fi->ch_mode + 1;
  49. fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
  50. } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
  51. fi->channels = 2;
  52. } else {
  53. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  54. "invalid channel mode: %d\n", fi->ch_mode);
  55. return -1;
  56. }
  57. /* bits per sample */
  58. bps_code = get_bits(gb, 3);
  59. if (bps_code == 3 || bps_code == 7) {
  60. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  61. "invalid sample size code (%d)\n",
  62. bps_code);
  63. return -1;
  64. }
  65. fi->bps = sample_size_table[bps_code];
  66. /* reserved bit */
  67. if (get_bits1(gb)) {
  68. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  69. "broken stream, invalid padding\n");
  70. return -1;
  71. }
  72. /* sample or frame count */
  73. fi->frame_or_sample_num = get_utf8(gb);
  74. if (fi->frame_or_sample_num < 0) {
  75. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  76. "sample/frame number invalid; utf8 fscked\n");
  77. return -1;
  78. }
  79. /* blocksize */
  80. if (bs_code == 0) {
  81. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  82. "reserved blocksize code: 0\n");
  83. return -1;
  84. } else if (bs_code == 6) {
  85. fi->blocksize = get_bits(gb, 8) + 1;
  86. } else if (bs_code == 7) {
  87. fi->blocksize = get_bits(gb, 16) + 1;
  88. } else {
  89. fi->blocksize = ff_flac_blocksize_table[bs_code];
  90. }
  91. /* sample rate */
  92. if (sr_code < 12) {
  93. fi->samplerate = ff_flac_sample_rate_table[sr_code];
  94. } else if (sr_code == 12) {
  95. fi->samplerate = get_bits(gb, 8) * 1000;
  96. } else if (sr_code == 13) {
  97. fi->samplerate = get_bits(gb, 16);
  98. } else if (sr_code == 14) {
  99. fi->samplerate = get_bits(gb, 16) * 10;
  100. } else {
  101. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  102. "illegal sample rate code %d\n",
  103. sr_code);
  104. return -1;
  105. }
  106. /* header CRC-8 check */
  107. skip_bits(gb, 8);
  108. if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
  109. get_bits_count(gb)/8)) {
  110. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  111. "header crc mismatch\n");
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
  117. {
  118. /* Technically, there is no limit to FLAC frame size, but an encoder
  119. should not write a frame that is larger than if verbatim encoding mode
  120. were to be used. */
  121. int count;
  122. count = 16; /* frame header */
  123. count += ch * ((7+bps+7)/8); /* subframe headers */
  124. if (ch == 2) {
  125. /* for stereo, need to account for using decorrelation */
  126. count += (( 2*bps+1) * blocksize + 7) / 8;
  127. } else {
  128. count += ( ch*bps * blocksize + 7) / 8;
  129. }
  130. count += 2; /* frame footer */
  131. return count;
  132. }