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.

227 lines
7.0KB

  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 "libavutil/log.h"
  23. #include "bytestream.h"
  24. #include "get_bits.h"
  25. #include "flac.h"
  26. #include "flacdata.h"
  27. static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
  28. static int64_t get_utf8(GetBitContext *gb)
  29. {
  30. int64_t val;
  31. GET_UTF8(val, get_bits(gb, 8), return -1;)
  32. return val;
  33. }
  34. int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
  35. FLACFrameInfo *fi, int log_level_offset)
  36. {
  37. int bs_code, sr_code, bps_code;
  38. /* frame sync code */
  39. if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
  40. av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
  41. return -1;
  42. }
  43. /* variable block size stream code */
  44. fi->is_var_size = get_bits1(gb);
  45. /* block size and sample rate codes */
  46. bs_code = get_bits(gb, 4);
  47. sr_code = get_bits(gb, 4);
  48. /* channels and decorrelation */
  49. fi->ch_mode = get_bits(gb, 4);
  50. if (fi->ch_mode < FLAC_MAX_CHANNELS) {
  51. fi->channels = fi->ch_mode + 1;
  52. fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
  53. } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
  54. fi->channels = 2;
  55. fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
  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. }
  137. int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
  138. enum FLACExtradataFormat *format,
  139. uint8_t **streaminfo_start)
  140. {
  141. if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
  142. av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
  143. return 0;
  144. }
  145. if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
  146. /* extradata contains STREAMINFO only */
  147. if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
  148. av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
  149. FLAC_STREAMINFO_SIZE-avctx->extradata_size);
  150. }
  151. *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
  152. *streaminfo_start = avctx->extradata;
  153. } else {
  154. if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
  155. av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
  156. return 0;
  157. }
  158. *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
  159. *streaminfo_start = &avctx->extradata[8];
  160. }
  161. return 1;
  162. }
  163. void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
  164. const uint8_t *buffer)
  165. {
  166. GetBitContext gb;
  167. init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
  168. skip_bits(&gb, 16); /* skip min blocksize */
  169. s->max_blocksize = get_bits(&gb, 16);
  170. if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
  171. av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
  172. s->max_blocksize);
  173. s->max_blocksize = 16;
  174. }
  175. skip_bits(&gb, 24); /* skip min frame size */
  176. s->max_framesize = get_bits_long(&gb, 24);
  177. s->samplerate = get_bits_long(&gb, 20);
  178. s->channels = get_bits(&gb, 3) + 1;
  179. s->bps = get_bits(&gb, 5) + 1;
  180. avctx->channels = s->channels;
  181. avctx->sample_rate = s->samplerate;
  182. avctx->bits_per_raw_sample = s->bps;
  183. s->samples = get_bits_long(&gb, 32) << 4;
  184. s->samples |= get_bits(&gb, 4);
  185. skip_bits_long(&gb, 64); /* md5 sum */
  186. skip_bits_long(&gb, 64); /* md5 sum */
  187. }
  188. void avpriv_flac_parse_block_header(const uint8_t *block_header,
  189. int *last, int *type, int *size)
  190. {
  191. int tmp = bytestream_get_byte(&block_header);
  192. if (last)
  193. *last = tmp & 0x80;
  194. if (type)
  195. *type = tmp & 0x7F;
  196. if (size)
  197. *size = bytestream_get_be24(&block_header);
  198. }