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.

162 lines
5.6KB

  1. /*
  2. * FLAC (Free Lossless Audio Codec) decoder/demuxer common functions
  3. * Copyright (c) 2008 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. /**
  22. * @file
  23. * FLAC (Free Lossless Audio Codec) decoder/demuxer common functions
  24. */
  25. #ifndef AVCODEC_FLAC_H
  26. #define AVCODEC_FLAC_H
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "get_bits.h"
  30. #define FLAC_STREAMINFO_SIZE 34
  31. #define FLAC_MAX_CHANNELS 8
  32. #define FLAC_MIN_BLOCKSIZE 16
  33. #define FLAC_MAX_BLOCKSIZE 65535
  34. #define FLAC_MIN_FRAME_SIZE 11
  35. enum {
  36. FLAC_CHMODE_INDEPENDENT = 0,
  37. FLAC_CHMODE_LEFT_SIDE = 1,
  38. FLAC_CHMODE_RIGHT_SIDE = 2,
  39. FLAC_CHMODE_MID_SIDE = 3,
  40. };
  41. enum {
  42. FLAC_METADATA_TYPE_STREAMINFO = 0,
  43. FLAC_METADATA_TYPE_PADDING,
  44. FLAC_METADATA_TYPE_APPLICATION,
  45. FLAC_METADATA_TYPE_SEEKTABLE,
  46. FLAC_METADATA_TYPE_VORBIS_COMMENT,
  47. FLAC_METADATA_TYPE_CUESHEET,
  48. FLAC_METADATA_TYPE_PICTURE,
  49. FLAC_METADATA_TYPE_INVALID = 127
  50. };
  51. enum FLACExtradataFormat {
  52. FLAC_EXTRADATA_FORMAT_STREAMINFO = 0,
  53. FLAC_EXTRADATA_FORMAT_FULL_HEADER = 1
  54. };
  55. #define FLACCOMMONINFO \
  56. int samplerate; /**< sample rate */\
  57. int channels; /**< number of channels */\
  58. int bps; /**< bits-per-sample */\
  59. /**
  60. * Data needed from the Streaminfo header for use by the raw FLAC demuxer
  61. * and/or the FLAC decoder.
  62. */
  63. #define FLACSTREAMINFO \
  64. FLACCOMMONINFO \
  65. int max_blocksize; /**< maximum block size, in samples */\
  66. int max_framesize; /**< maximum frame size, in bytes */\
  67. int64_t samples; /**< total number of samples */\
  68. typedef struct FLACStreaminfo {
  69. FLACSTREAMINFO
  70. } FLACStreaminfo;
  71. typedef struct FLACFrameInfo {
  72. FLACCOMMONINFO
  73. int blocksize; /**< block size of the frame */
  74. int ch_mode; /**< channel decorrelation mode */
  75. int64_t frame_or_sample_num; /**< frame number or sample number */
  76. int is_var_size; /**< specifies if the stream uses variable
  77. block sizes or a fixed block size;
  78. also determines the meaning of
  79. frame_or_sample_num */
  80. } FLACFrameInfo;
  81. /**
  82. * Parse the Streaminfo metadata block
  83. * @param[out] avctx codec context to set basic stream parameters
  84. * @param[out] s where parsed information is stored
  85. * @param[in] buffer pointer to start of 34-byte streaminfo data
  86. */
  87. void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
  88. const uint8_t *buffer);
  89. #if LIBAVCODEC_VERSION_MAJOR < 57
  90. void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
  91. const uint8_t *buffer);
  92. int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
  93. enum FLACExtradataFormat *format,
  94. uint8_t **streaminfo_start);
  95. #endif
  96. /**
  97. * Validate the FLAC extradata.
  98. * @param[in] avctx codec context containing the extradata.
  99. * @param[out] format extradata format.
  100. * @param[out] streaminfo_start pointer to start of 34-byte STREAMINFO data.
  101. * @return 1 if valid, 0 if not valid.
  102. */
  103. int ff_flac_is_extradata_valid(AVCodecContext *avctx,
  104. enum FLACExtradataFormat *format,
  105. uint8_t **streaminfo_start);
  106. /**
  107. * Calculate an estimate for the maximum frame size based on verbatim mode.
  108. * @param blocksize block size, in samples
  109. * @param ch number of channels
  110. * @param bps bits-per-sample
  111. */
  112. int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
  113. /**
  114. * Validate and decode a frame header.
  115. * @param avctx AVCodecContext to use as av_log() context
  116. * @param gb GetBitContext from which to read frame header
  117. * @param[out] fi frame information
  118. * @param log_level_offset log level offset. can be used to silence error messages.
  119. * @return non-zero on error, 0 if ok
  120. */
  121. int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
  122. FLACFrameInfo *fi, int log_level_offset);
  123. void ff_flac_set_channel_layout(AVCodecContext *avctx);
  124. /**
  125. * Parse the metadata block parameters from the header.
  126. * @param[in] block_header header data, at least 4 bytes
  127. * @param[out] last indicator for last metadata block
  128. * @param[out] type metadata block type
  129. * @param[out] size metadata block size
  130. */
  131. static av_always_inline void flac_parse_block_header(const uint8_t *block_header,
  132. int *last, int *type, int *size)
  133. {
  134. int tmp = bytestream_get_byte(&block_header);
  135. if (last)
  136. *last = tmp & 0x80;
  137. if (type)
  138. *type = tmp & 0x7F;
  139. if (size)
  140. *size = bytestream_get_be24(&block_header);
  141. }
  142. #endif /* AVCODEC_FLAC_H */