FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tak.c
Go to the documentation of this file.
1 /*
2  * TAK common code
3  * Copyright (c) 2012 Paul B Mahol
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 #include "libavutil/crc.h"
23 #include "libavutil/intreadwrite.h"
24 #include "tak.h"
25 
26 static const int64_t tak_channel_layouts[] = {
27  0,
46 };
47 
48 static const uint16_t frame_duration_type_quants[] = {
49  3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
50 };
51 
53 {
54  int nb_samples, max_nb_samples;
55 
56  if (type <= TAK_FST_250ms) {
57  nb_samples = sample_rate * frame_duration_type_quants[type] >>
59  max_nb_samples = 16384;
60  } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
61  nb_samples = frame_duration_type_quants[type];
62  max_nb_samples = sample_rate *
65  } else {
66  return AVERROR_INVALIDDATA;
67  }
68 
69  if (nb_samples <= 0 || nb_samples > max_nb_samples)
70  return AVERROR_INVALIDDATA;
71 
72  return nb_samples;
73 }
74 
75 int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
76 {
77  uint32_t crc, CRC;
78 
79  if (buf_size < 4)
80  return AVERROR_INVALIDDATA;
81  buf_size -= 3;
82 
83  CRC = AV_RB24(buf + buf_size);
84  crc = av_crc(av_crc_get_table(AV_CRC_24_IEEE), 0xCE04B7U, buf, buf_size);
85  if (CRC != crc)
86  return AVERROR_INVALIDDATA;
87 
88  return 0;
89 }
90 
92 {
93  uint64_t channel_mask = 0;
94  int frame_type, i;
95 
98 
99  frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
101 
105  s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
106  TAK_BPS_MIN;
109 
110  if (get_bits1(gb)) {
112  if (get_bits1(gb)) {
113  for (i = 0; i < s->channels; i++) {
115 
116  if (value < FF_ARRAY_ELEMS(tak_channel_layouts))
117  channel_mask |= tak_channel_layouts[value];
118  }
119  }
120  }
121 
122  s->ch_layout = channel_mask;
123  s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
124 }
125 
127  TAKStreamInfo *ti, int log_level_offset)
128 {
130  av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
131  return AVERROR_INVALIDDATA;
132  }
133 
136 
137  if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
139  skip_bits(gb, 2);
140  } else {
141  ti->last_frame_samples = 0;
142  }
143 
144  if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
146 
147  if (get_bits(gb, 6))
148  skip_bits(gb, 25);
149  align_get_bits(gb);
150  }
151 
153  return AVERROR_INVALIDDATA;
154 
155  skip_bits(gb, 24);
156 
157  return 0;
158 }
static const uint16_t frame_duration_type_quants[]
Definition: tak.c:48
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, TAKStreamInfo *ti, int log_level_offset)
Validate and decode a frame header.
Definition: tak.c:126
#define AV_CH_TOP_FRONT_RIGHT
int channels
Definition: tak.h:134
#define TAK_FORMAT_BPS_BITS
Definition: tak.h:38
#define TAK_BPS_MIN
Definition: tak.h:51
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
#define AV_CH_TOP_FRONT_LEFT
#define TAK_FRAME_HEADER_NO_BITS
Definition: tak.h:56
static const int64_t tak_channel_layouts[]
Definition: tak.c:26
#define AV_CH_TOP_FRONT_CENTER
#define TAK_FRAME_FLAG_IS_LAST
Definition: tak.h:61
uint64_t ch_layout
Definition: tak.h:139
#define TAK_SIZE_FRAME_DURATION_BITS
Definition: tak.h:42
#define TAK_FORMAT_DATA_TYPE_BITS
Definition: tak.h:36
uint8_t
#define AV_CH_TOP_BACK_LEFT
int flags
Definition: tak.h:130
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:75
#define AV_CH_TOP_BACK_CENTER
#define AV_CH_LOW_FREQUENCY
#define TAK_FRAME_HEADER_FLAGS_BITS
Definition: tak.h:52
#define TAK_FRAME_FLAG_HAS_INFO
Definition: tak.h:62
#define AV_CH_BACK_LEFT
int data_type
Definition: tak.h:132
#define av_log(a,...)
#define TAK_FRAME_HEADER_SYNC_ID
Definition: tak.h:53
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition: get_bits.h:352
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int last_frame_samples
Definition: tak.h:138
#define TAK_SIZE_SAMPLES_NUM_BITS
Definition: tak.h:43
int bps
Definition: tak.h:135
int64_t samples
Definition: tak.h:140
#define AV_CH_TOP_CENTER
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:357
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_CENTER
#define TAK_CHANNELS_MIN
Definition: tak.h:50
#define FF_ARRAY_ELEMS(a)
#define AV_CH_FRONT_RIGHT_OF_CENTER
sample_rate
#define TAK_FRAME_FLAG_HAS_METADATA
Definition: tak.h:63
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
Parse the Streaminfo metadata block.
Definition: tak.c:91
main external API structure.
Definition: avcodec.h:1648
#define AV_CH_FRONT_LEFT
enum TAKCodecType codec
Definition: tak.h:131
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
#define AV_CH_TOP_BACK_RIGHT
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
#define TAK_SAMPLE_RATE_MIN
Definition: tak.h:49
int frame_samples
Definition: tak.h:137
TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions.
int sample_rate
Definition: tak.h:133
#define TAK_ENCODER_CODEC_BITS
Definition: tak.h:46
int frame_num
Definition: tak.h:136
#define AV_CH_BACK_CENTER
#define AV_CH_SIDE_RIGHT
TAKFrameSizeType
Definition: tak.h:116
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:343
#define TAK_ENCODER_PROFILE_BITS
Definition: tak.h:47
#define TAK_FORMAT_CH_LAYOUT_BITS
Definition: tak.h:41
#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS
Definition: tak.h:55
#define TAK_FRAME_HEADER_SYNC_ID_BITS
Definition: tak.h:54
#define TAK_FRAME_DURATION_QUANT_SHIFT
Definition: tak.h:57
#define TAK_FORMAT_VALID_BITS
Definition: tak.h:40
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:445
#define AV_CH_FRONT_RIGHT
#define AV_CH_SIDE_LEFT
#define TAK_FORMAT_CHANNEL_BITS
Definition: tak.h:39
static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
Definition: tak.c:52
#define TAK_FORMAT_SAMPLE_RATE_BITS
Definition: tak.h:37
#define AV_CH_BACK_RIGHT