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.

254 lines
8.0KB

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