FFmpeg
flac.h
Go to the documentation of this file.
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 /**
23  * @file
24  * FLAC (Free Lossless Audio Codec) decoder/demuxer common functions
25  */
26 
27 #ifndef AVCODEC_FLAC_H
28 #define AVCODEC_FLAC_H
29 
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "get_bits.h"
33 
34 #define FLAC_STREAMINFO_SIZE 34
35 #define FLAC_MAX_CHANNELS 8
36 #define FLAC_MIN_BLOCKSIZE 16
37 #define FLAC_MAX_BLOCKSIZE 65535
38 #define FLAC_MIN_FRAME_SIZE 11
39 
40 enum {
45 };
46 
47 enum {
56 };
57 
61 };
62 
63 #define FLACCOMMONINFO \
64  int samplerate; /**< sample rate */\
65  int channels; /**< number of channels */\
66  int bps; /**< bits-per-sample */\
67 
68 /**
69  * Data needed from the Streaminfo header for use by the raw FLAC demuxer
70  * and/or the FLAC decoder.
71  */
72 #define FLACSTREAMINFO \
73  FLACCOMMONINFO \
74  int max_blocksize; /**< maximum block size, in samples */\
75  int max_framesize; /**< maximum frame size, in bytes */\
76  int64_t samples; /**< total number of samples */\
77 
78 typedef struct FLACStreaminfo {
81 
82 typedef struct FLACFrameInfo {
84  int blocksize; /**< block size of the frame */
85  int ch_mode; /**< channel decorrelation mode */
86  int64_t frame_or_sample_num; /**< frame number or sample number */
87  int is_var_size; /**< specifies if the stream uses variable
88  block sizes or a fixed block size;
89  also determines the meaning of
90  frame_or_sample_num */
92 
93 /**
94  * Parse the Streaminfo metadata block
95  * @param[out] avctx codec context to set basic stream parameters
96  * @param[out] s where parsed information is stored
97  * @param[in] buffer pointer to start of 34-byte streaminfo data
98  *
99  * @return negative error code on faiure or >= 0 on success
100  */
102  const uint8_t *buffer);
103 
104 /**
105  * Validate the FLAC extradata.
106  * @param[in] avctx codec context containing the extradata.
107  * @param[out] format extradata format.
108  * @param[out] streaminfo_start pointer to start of 34-byte STREAMINFO data.
109  * @return 1 if valid, 0 if not valid.
110  */
113  uint8_t **streaminfo_start);
114 
115 /**
116  * Calculate an estimate for the maximum frame size based on verbatim mode.
117  * @param blocksize block size, in samples
118  * @param ch number of channels
119  * @param bps bits-per-sample
120  */
121 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
122 
123 /**
124  * Validate and decode a frame header.
125  * @param avctx AVCodecContext to use as av_log() context
126  * @param gb GetBitContext from which to read frame header
127  * @param[out] fi frame information
128  * @param log_level_offset log level offset. can be used to silence error messages.
129  * @return non-zero on error, 0 if ok
130  */
132  FLACFrameInfo *fi, int log_level_offset);
133 
135 
136 /**
137  * Parse the metadata block parameters from the header.
138  * @param[in] block_header header data, at least 4 bytes
139  * @param[out] last indicator for last metadata block
140  * @param[out] type metadata block type
141  * @param[out] size metadata block size
142  */
143 static av_always_inline void flac_parse_block_header(const uint8_t *block_header,
144  int *last, int *type, int *size)
145 {
146  int tmp = bytestream_get_byte(&block_header);
147  if (last)
148  *last = tmp & 0x80;
149  if (type)
150  *type = tmp & 0x7F;
151  if (size)
152  *size = bytestream_get_be24(&block_header);
153 }
154 
155 #endif /* AVCODEC_FLAC_H */
FLAC_CHMODE_MID_SIDE
@ FLAC_CHMODE_MID_SIDE
Definition: flac.h:44
ff_flac_decode_frame_header
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
Definition: flac.c:50
FLAC_METADATA_TYPE_SEEKTABLE
@ FLAC_METADATA_TYPE_SEEKTABLE
Definition: flac.h:51
ff_flac_set_channel_layout
void ff_flac_set_channel_layout(AVCodecContext *avctx)
Definition: flac.c:196
ch
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8),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_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
FLACFrameInfo::frame_or_sample_num
int64_t frame_or_sample_num
frame number or sample number
Definition: flac.h:88
FLACFrameInfo::is_var_size
int is_var_size
specifies if the stream uses variable block sizes or a fixed block size; also determines the meaning ...
Definition: flac.h:89
GetBitContext
Definition: get_bits.h:61
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
FLACCOMMONINFO
#define FLACCOMMONINFO
bits-per-sample
Definition: flac.h:63
FLACStreaminfo
Definition: flac.h:80
s
#define s(width, name)
Definition: cbs_vp9.c:257
FLAC_METADATA_TYPE_CUESHEET
@ FLAC_METADATA_TYPE_CUESHEET
Definition: flac.h:53
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
get_bits.h
FLACExtradataFormat
FLACExtradataFormat
Definition: flac.h:58
FLAC_METADATA_TYPE_STREAMINFO
@ FLAC_METADATA_TYPE_STREAMINFO
Definition: flac.h:48
FLAC_METADATA_TYPE_PICTURE
@ FLAC_METADATA_TYPE_PICTURE
Definition: flac.h:54
FLACFrameInfo::blocksize
FLACCOMMONINFO int blocksize
block size of the frame
Definition: flac.h:86
flac_parse_block_header
static av_always_inline void flac_parse_block_header(const uint8_t *block_header, int *last, int *type, int *size)
Parse the metadata block parameters from the header.
Definition: flac.h:145
ff_flac_get_max_frame_size
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
Calculate an estimate for the maximum frame size based on verbatim mode.
Definition: flac.c:148
ff_flac_is_extradata_valid
int ff_flac_is_extradata_valid(AVCodecContext *avctx, enum FLACExtradataFormat *format, uint8_t **streaminfo_start)
Validate the FLAC extradata.
Definition: flac.c:169
FLAC_CHMODE_RIGHT_SIDE
@ FLAC_CHMODE_RIGHT_SIDE
Definition: flac.h:43
FLAC_METADATA_TYPE_APPLICATION
@ FLAC_METADATA_TYPE_APPLICATION
Definition: flac.h:50
FLACFrameInfo
Definition: flac.h:84
bps
unsigned bps
Definition: movenc.c:1497
size
int size
Definition: twinvq_data.h:11134
fi
AVS_FilterInfo ** fi
Definition: avisynth_c.h:807
FLAC_EXTRADATA_FORMAT_FULL_HEADER
@ FLAC_EXTRADATA_FORMAT_FULL_HEADER
Definition: flac.h:60
FLAC_METADATA_TYPE_INVALID
@ FLAC_METADATA_TYPE_INVALID
Definition: flac.h:55
FLACFrameInfo::ch_mode
int ch_mode
channel decorrelation mode
Definition: flac.h:87
av_always_inline
#define av_always_inline
Definition: attributes.h:43
uint8_t
uint8_t
Definition: audio_convert.c:194
ff_flac_parse_streaminfo
int ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, const uint8_t *buffer)
Parse the Streaminfo metadata block.
Definition: flac.c:204
avcodec.h
FLAC_METADATA_TYPE_VORBIS_COMMENT
@ FLAC_METADATA_TYPE_VORBIS_COMMENT
Definition: flac.h:52
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
FLAC_EXTRADATA_FORMAT_STREAMINFO
@ FLAC_EXTRADATA_FORMAT_STREAMINFO
Definition: flac.h:59
FLAC_METADATA_TYPE_PADDING
@ FLAC_METADATA_TYPE_PADDING
Definition: flac.h:49
FLAC_CHMODE_LEFT_SIDE
@ FLAC_CHMODE_LEFT_SIDE
Definition: flac.h:42
bytestream.h
FLACSTREAMINFO
#define FLACSTREAMINFO
Data needed from the Streaminfo header for use by the raw FLAC demuxer and/or the FLAC decoder.
Definition: flac.h:73
FLAC_CHMODE_INDEPENDENT
@ FLAC_CHMODE_INDEPENDENT
Definition: flac.h:41