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 static int crc_init = 0;
76 #if CONFIG_SMALL
77 #define CRC_TABLE_SIZE 257
78 #else
79 #define CRC_TABLE_SIZE 1024
80 #endif
82 
84 {
85  if (!crc_init) {
86  av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
87  crc_init = 1;
88  }
89 }
90 
91 int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
92 {
93  uint32_t crc, CRC;
94 
95  if (buf_size < 4)
96  return AVERROR_INVALIDDATA;
97  buf_size -= 3;
98 
99  CRC = AV_RB24(buf + buf_size);
100  crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
101  if (CRC != crc)
102  return AVERROR_INVALIDDATA;
103 
104  return 0;
105 }
106 
108 {
109  uint64_t channel_mask = 0;
110  int frame_type, i;
111 
114 
115  frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
117 
121  s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
122  TAK_BPS_MIN;
125 
126  if (get_bits1(gb)) {
128  if (get_bits1(gb)) {
129  for (i = 0; i < s->channels; i++) {
131 
132  if (value < FF_ARRAY_ELEMS(tak_channel_layouts))
133  channel_mask |= tak_channel_layouts[value];
134  }
135  }
136  }
137 
138  s->ch_layout = channel_mask;
139  s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
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) {
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 }