FFmpeg
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 
23 #include "libavutil/crc.h"
24 #include "libavutil/intreadwrite.h"
25 
26 #define BITSTREAM_READER_LE
27 #include "tak.h"
28 
29 static const int64_t tak_channel_layouts[] = {
30  0,
49 };
50 
51 static const uint16_t frame_duration_type_quants[] = {
52  3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
53 };
54 
56 {
57  int nb_samples, max_nb_samples;
58 
59  if (type <= TAK_FST_250ms) {
62  max_nb_samples = 16384;
64  nb_samples = frame_duration_type_quants[type];
65  max_nb_samples = sample_rate *
68  } else {
69  return AVERROR_INVALIDDATA;
70  }
71 
72  if (nb_samples <= 0 || nb_samples > max_nb_samples)
73  return AVERROR_INVALIDDATA;
74 
75  return nb_samples;
76 }
77 
78 int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
79 {
80  uint32_t crc, CRC;
81 
82  if (buf_size < 4)
83  return AVERROR_INVALIDDATA;
84  buf_size -= 3;
85 
86  CRC = AV_RB24(buf + buf_size);
87  crc = av_crc(av_crc_get_table(AV_CRC_24_IEEE), 0xCE04B7U, buf, buf_size);
88  if (CRC != crc)
89  return AVERROR_INVALIDDATA;
90 
91  return 0;
92 }
93 
95 {
96  uint64_t channel_mask = 0;
97  int frame_type, i;
98 
99  s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS);
101 
103  s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS);
104 
105  s->data_type = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
106  s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) +
108  s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
109  TAK_BPS_MIN;
110  s->channels = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) +
112 
113  if (get_bits1(gb)) {
115  if (get_bits1(gb)) {
116  for (i = 0; i < s->channels; i++) {
118 
120  channel_mask |= tak_channel_layouts[value];
121  }
122  }
123  }
124 
125  s->ch_layout = channel_mask;
126  s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
127 }
128 
129 int avpriv_tak_parse_streaminfo(TAKStreamInfo *s, const uint8_t *buf, int size)
130 {
131  GetBitContext gb;
132  int ret = init_get_bits8(&gb, buf, size);
133 
134  if (ret < 0)
135  return AVERROR_INVALIDDATA;
136 
138 
139  return 0;
140 }
141 
143  TAKStreamInfo *ti, int log_level_offset)
144 {
146  av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
147  return AVERROR_INVALIDDATA;
148  }
149 
152 
153  if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
155  skip_bits(gb, 2);
156  } else {
157  ti->last_frame_samples = 0;
158  }
159 
160  if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
161  ff_tak_parse_streaminfo(ti, gb);
162 
163  if (get_bits(gb, 6))
164  skip_bits(gb, 25);
165  align_get_bits(gb);
166  }
167 
169  return AVERROR_INVALIDDATA;
170 
171  skip_bits(gb, 24);
172 
173  return 0;
174 }
tak_channel_layouts
static const int64_t tak_channel_layouts[]
Definition: tak.c:29
AV_CH_TOP_FRONT_CENTER
#define AV_CH_TOP_FRONT_CENTER
Definition: channel_layout.h:62
TAK_FRAME_HEADER_NO_BITS
#define TAK_FRAME_HEADER_NO_BITS
Definition: tak.h:55
ff_tak_decode_frame_header
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:142
AV_CH_TOP_FRONT_RIGHT
#define AV_CH_TOP_FRONT_RIGHT
Definition: channel_layout.h:63
TAK_FRAME_HEADER_SYNC_ID
#define TAK_FRAME_HEADER_SYNC_ID
Definition: tak.h:52
TAKStreamInfo::flags
int flags
Definition: tak.h:129
AV_CRC_24_IEEE
@ AV_CRC_24_IEEE
Definition: crc.h:55
TAK_FORMAT_CH_LAYOUT_BITS
#define TAK_FORMAT_CH_LAYOUT_BITS
Definition: tak.h:40
AV_CH_TOP_FRONT_LEFT
#define AV_CH_TOP_FRONT_LEFT
Definition: channel_layout.h:61
TAKStreamInfo::last_frame_samples
int last_frame_samples
Definition: tak.h:137
TAK_BPS_MIN
#define TAK_BPS_MIN
Definition: tak.h:50
sample_rate
sample_rate
Definition: ffmpeg_filter.c:156
AV_CH_TOP_BACK_LEFT
#define AV_CH_TOP_BACK_LEFT
Definition: channel_layout.h:64
TAK_SIZE_FRAME_DURATION_BITS
#define TAK_SIZE_FRAME_DURATION_BITS
Definition: tak.h:41
crc.h
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:468
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
AV_CH_TOP_BACK_CENTER
#define AV_CH_TOP_BACK_CENTER
Definition: channel_layout.h:65
TAK_FRAME_FLAG_HAS_INFO
#define TAK_FRAME_FLAG_HAS_INFO
Definition: tak.h:61
GetBitContext
Definition: get_bits.h:62
AV_CH_BACK_LEFT
#define AV_CH_BACK_LEFT
Definition: channel_layout.h:53
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
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:52
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
frame_duration_type_quants
static const uint16_t frame_duration_type_quants[]
Definition: tak.c:51
TAK_FORMAT_BPS_BITS
#define TAK_FORMAT_BPS_BITS
Definition: tak.h:37
ff_tak_parse_streaminfo
void ff_tak_parse_streaminfo(TAKStreamInfo *s, GetBitContext *gb)
Definition: tak.c:94
TAK_FRAME_FLAG_IS_LAST
#define TAK_FRAME_FLAG_IS_LAST
Definition: tak.h:60
TAK_FORMAT_SAMPLE_RATE_BITS
#define TAK_FORMAT_SAMPLE_RATE_BITS
Definition: tak.h:36
TAK_FRAME_FLAG_HAS_METADATA
#define TAK_FRAME_FLAG_HAS_METADATA
Definition: tak.h:62
AV_CH_TOP_CENTER
#define AV_CH_TOP_CENTER
Definition: channel_layout.h:60
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:499
tak.h
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:51
AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
Definition: channel_layout.h:55
TAKStreamInfo::frame_num
int frame_num
Definition: tak.h:135
TAK_FORMAT_CHANNEL_BITS
#define TAK_FORMAT_CHANNEL_BITS
Definition: tak.h:38
TAK_FST_250ms
@ TAK_FST_250ms
Definition: tak.h:119
size
int size
Definition: twinvq_data.h:10344
ff_tak_check_crc
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:78
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_BACK_RIGHT
Definition: channel_layout.h:66
AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
Definition: channel_layout.h:56
get_bits64
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition: get_bits.h:573
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
frame_type
frame_type
Definition: jpeg2000_parser.c:31
AV_CH_BACK_CENTER
#define AV_CH_BACK_CENTER
Definition: channel_layout.h:57
value
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 default value
Definition: writing_filters.txt:86
AV_CH_FRONT_LEFT
#define AV_CH_FRONT_LEFT
Definition: channel_layout.h:49
tak_get_nb_samples
static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
Definition: tak.c:55
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:59
TAK_FRAME_HEADER_FLAGS_BITS
#define TAK_FRAME_HEADER_FLAGS_BITS
Definition: tak.h:51
ret
ret
Definition: filter_design.txt:187
avpriv_tak_parse_streaminfo
int avpriv_tak_parse_streaminfo(TAKStreamInfo *s, const uint8_t *buf, int size)
Parse the Streaminfo metadata block.
Definition: tak.c:129
TAK_FRAME_HEADER_SYNC_ID_BITS
#define TAK_FRAME_HEADER_SYNC_ID_BITS
Definition: tak.h:53
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:694
AVCodecContext
main external API structure.
Definition: avcodec.h:383
channel_layout.h
av_crc
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:392
TAK_SAMPLE_RATE_MIN
#define TAK_SAMPLE_RATE_MIN
Definition: tak.h:48
TAK_FORMAT_VALID_BITS
#define TAK_FORMAT_VALID_BITS
Definition: tak.h:39
TAK_ENCODER_PROFILE_BITS
#define TAK_ENCODER_PROFILE_BITS
Definition: tak.h:46
AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT
Definition: channel_layout.h:50
TAK_CHANNELS_MIN
#define TAK_CHANNELS_MIN
Definition: tak.h:49
TAKStreamInfo
Definition: tak.h:128
TAK_FRAME_DURATION_QUANT_SHIFT
#define TAK_FRAME_DURATION_QUANT_SHIFT
Definition: tak.h:56
TAK_FRAME_HEADER_SAMPLE_COUNT_BITS
#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS
Definition: tak.h:54
TAKFrameSizeType
TAKFrameSizeType
Definition: tak.h:115
TAK_SIZE_SAMPLES_NUM_BITS
#define TAK_SIZE_SAMPLES_NUM_BITS
Definition: tak.h:42
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CH_BACK_RIGHT
#define AV_CH_BACK_RIGHT
Definition: channel_layout.h:54
TAK_FORMAT_DATA_TYPE_BITS
#define TAK_FORMAT_DATA_TYPE_BITS
Definition: tak.h:35
AV_RB24
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:97
AV_CH_SIDE_LEFT
#define AV_CH_SIDE_LEFT
Definition: channel_layout.h:58
TAK_ENCODER_CODEC_BITS
#define TAK_ENCODER_CODEC_BITS
Definition: tak.h:45