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.

246 lines
7.6KB

  1. /*
  2. * FLAC common code
  3. * Copyright (c) 2009 Justin Ruggles
  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. #include "libavutil/channel_layout.h"
  22. #include "libavutil/crc.h"
  23. #include "libavutil/log.h"
  24. #include "bytestream.h"
  25. #include "get_bits.h"
  26. #include "flac.h"
  27. #include "flacdata.h"
  28. static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
  29. static const int64_t flac_channel_layouts[6] = {
  30. AV_CH_LAYOUT_MONO,
  31. AV_CH_LAYOUT_STEREO,
  32. AV_CH_LAYOUT_SURROUND,
  33. AV_CH_LAYOUT_QUAD,
  34. AV_CH_LAYOUT_5POINT0,
  35. AV_CH_LAYOUT_5POINT1
  36. };
  37. static int64_t get_utf8(GetBitContext *gb)
  38. {
  39. int64_t val;
  40. GET_UTF8(val, get_bits(gb, 8), return -1;)
  41. return val;
  42. }
  43. int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
  44. FLACFrameInfo *fi, int log_level_offset)
  45. {
  46. int bs_code, sr_code, bps_code;
  47. /* frame sync code */
  48. if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
  49. av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
  50. return -1;
  51. }
  52. /* variable block size stream code */
  53. fi->is_var_size = get_bits1(gb);
  54. /* block size and sample rate codes */
  55. bs_code = get_bits(gb, 4);
  56. sr_code = get_bits(gb, 4);
  57. /* channels and decorrelation */
  58. fi->ch_mode = get_bits(gb, 4);
  59. if (fi->ch_mode < FLAC_MAX_CHANNELS) {
  60. fi->channels = fi->ch_mode + 1;
  61. fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
  62. } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
  63. fi->channels = 2;
  64. fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
  65. } else {
  66. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  67. "invalid channel mode: %d\n", fi->ch_mode);
  68. return -1;
  69. }
  70. /* bits per sample */
  71. bps_code = get_bits(gb, 3);
  72. if (bps_code == 3 || bps_code == 7) {
  73. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  74. "invalid sample size code (%d)\n",
  75. bps_code);
  76. return -1;
  77. }
  78. fi->bps = sample_size_table[bps_code];
  79. /* reserved bit */
  80. if (get_bits1(gb)) {
  81. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  82. "broken stream, invalid padding\n");
  83. return -1;
  84. }
  85. /* sample or frame count */
  86. fi->frame_or_sample_num = get_utf8(gb);
  87. if (fi->frame_or_sample_num < 0) {
  88. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  89. "sample/frame number invalid; utf8 fscked\n");
  90. return -1;
  91. }
  92. /* blocksize */
  93. if (bs_code == 0) {
  94. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  95. "reserved blocksize code: 0\n");
  96. return -1;
  97. } else if (bs_code == 6) {
  98. fi->blocksize = get_bits(gb, 8) + 1;
  99. } else if (bs_code == 7) {
  100. fi->blocksize = get_bits(gb, 16) + 1;
  101. } else {
  102. fi->blocksize = ff_flac_blocksize_table[bs_code];
  103. }
  104. /* sample rate */
  105. if (sr_code < 12) {
  106. fi->samplerate = ff_flac_sample_rate_table[sr_code];
  107. } else if (sr_code == 12) {
  108. fi->samplerate = get_bits(gb, 8) * 1000;
  109. } else if (sr_code == 13) {
  110. fi->samplerate = get_bits(gb, 16);
  111. } else if (sr_code == 14) {
  112. fi->samplerate = get_bits(gb, 16) * 10;
  113. } else {
  114. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  115. "illegal sample rate code %d\n",
  116. sr_code);
  117. return -1;
  118. }
  119. /* header CRC-8 check */
  120. skip_bits(gb, 8);
  121. if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
  122. get_bits_count(gb)/8)) {
  123. av_log(avctx, AV_LOG_ERROR + log_level_offset,
  124. "header crc mismatch\n");
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
  130. {
  131. /* Technically, there is no limit to FLAC frame size, but an encoder
  132. should not write a frame that is larger than if verbatim encoding mode
  133. were to be used. */
  134. int count;
  135. count = 16; /* frame header */
  136. count += ch * ((7+bps+7)/8); /* subframe headers */
  137. if (ch == 2) {
  138. /* for stereo, need to account for using decorrelation */
  139. count += (( 2*bps+1) * blocksize + 7) / 8;
  140. } else {
  141. count += ( ch*bps * blocksize + 7) / 8;
  142. }
  143. count += 2; /* frame footer */
  144. return count;
  145. }
  146. int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
  147. enum FLACExtradataFormat *format,
  148. uint8_t **streaminfo_start)
  149. {
  150. if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
  151. av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
  152. return 0;
  153. }
  154. if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
  155. /* extradata contains STREAMINFO only */
  156. if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
  157. av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
  158. FLAC_STREAMINFO_SIZE-avctx->extradata_size);
  159. }
  160. *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
  161. *streaminfo_start = avctx->extradata;
  162. } else {
  163. if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
  164. av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
  165. return 0;
  166. }
  167. *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
  168. *streaminfo_start = &avctx->extradata[8];
  169. }
  170. return 1;
  171. }
  172. void ff_flac_set_channel_layout(AVCodecContext *avctx)
  173. {
  174. if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
  175. avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
  176. else
  177. avctx->channel_layout = 0;
  178. }
  179. void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
  180. const uint8_t *buffer)
  181. {
  182. GetBitContext gb;
  183. init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
  184. skip_bits(&gb, 16); /* skip min blocksize */
  185. s->max_blocksize = get_bits(&gb, 16);
  186. if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
  187. av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
  188. s->max_blocksize);
  189. s->max_blocksize = 16;
  190. }
  191. skip_bits(&gb, 24); /* skip min frame size */
  192. s->max_framesize = get_bits_long(&gb, 24);
  193. s->samplerate = get_bits_long(&gb, 20);
  194. s->channels = get_bits(&gb, 3) + 1;
  195. s->bps = get_bits(&gb, 5) + 1;
  196. avctx->channels = s->channels;
  197. avctx->sample_rate = s->samplerate;
  198. avctx->bits_per_raw_sample = s->bps;
  199. ff_flac_set_channel_layout(avctx);
  200. s->samples = get_bits_long(&gb, 32) << 4;
  201. s->samples |= get_bits(&gb, 4);
  202. skip_bits_long(&gb, 64); /* md5 sum */
  203. skip_bits_long(&gb, 64); /* md5 sum */
  204. }
  205. void avpriv_flac_parse_block_header(const uint8_t *block_header,
  206. int *last, int *type, int *size)
  207. {
  208. int tmp = bytestream_get_byte(&block_header);
  209. if (last)
  210. *last = tmp & 0x80;
  211. if (type)
  212. *type = tmp & 0x7F;
  213. if (size)
  214. *size = bytestream_get_be24(&block_header);
  215. }