flac.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * FLAC (Free Lossless Audio Codec) decoder/demuxer common functions
  3. * Copyright (c) 2008 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. /**
  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. * @return negative error code on faiure or >= 0 on success
  88. */
  89. int ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
  90. const uint8_t *buffer);
  91. /**
  92. * Validate the FLAC extradata.
  93. * @param[in] avctx codec context containing the extradata.
  94. * @param[out] format extradata format.
  95. * @param[out] streaminfo_start pointer to start of 34-byte STREAMINFO data.
  96. * @return 1 if valid, 0 if not valid.
  97. */
  98. int ff_flac_is_extradata_valid(AVCodecContext *avctx,
  99. enum FLACExtradataFormat *format,
  100. uint8_t **streaminfo_start);
  101. /**
  102. * Calculate an estimate for the maximum frame size based on verbatim mode.
  103. * @param blocksize block size, in samples
  104. * @param ch number of channels
  105. * @param bps bits-per-sample
  106. */
  107. int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
  108. /**
  109. * Validate and decode a frame header.
  110. * @param avctx AVCodecContext to use as av_log() context
  111. * @param gb GetBitContext from which to read frame header
  112. * @param[out] fi frame information
  113. * @param log_level_offset log level offset. can be used to silence error messages.
  114. * @return non-zero on error, 0 if ok
  115. */
  116. int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
  117. FLACFrameInfo *fi, int log_level_offset);
  118. void ff_flac_set_channel_layout(AVCodecContext *avctx);
  119. /**
  120. * Parse the metadata block parameters from the header.
  121. * @param[in] block_header header data, at least 4 bytes
  122. * @param[out] last indicator for last metadata block
  123. * @param[out] type metadata block type
  124. * @param[out] size metadata block size
  125. */
  126. static av_always_inline void flac_parse_block_header(const uint8_t *block_header,
  127. int *last, int *type, int *size)
  128. {
  129. int tmp = bytestream_get_byte(&block_header);
  130. if (last)
  131. *last = tmp & 0x80;
  132. if (type)
  133. *type = tmp & 0x7F;
  134. if (size)
  135. *size = bytestream_get_be24(&block_header);
  136. }
  137. #endif /* AVCODEC_FLAC_H */