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.

156 lines
4.7KB

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