1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * TAK decoder/demuxer common code
3cabdff1aSopenharmony_ci * Copyright (c) 2012 Paul B Mahol
4cabdff1aSopenharmony_ci *
5cabdff1aSopenharmony_ci * This file is part of FFmpeg.
6cabdff1aSopenharmony_ci *
7cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
8cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
9cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
10cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
11cabdff1aSopenharmony_ci *
12cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
13cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
14cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15cabdff1aSopenharmony_ci * Lesser General Public License for more details.
16cabdff1aSopenharmony_ci *
17cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
18cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
19cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20cabdff1aSopenharmony_ci */
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci/**
23cabdff1aSopenharmony_ci * @file
24cabdff1aSopenharmony_ci * TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions
25cabdff1aSopenharmony_ci */
26cabdff1aSopenharmony_ci
27cabdff1aSopenharmony_ci#ifndef AVCODEC_TAK_H
28cabdff1aSopenharmony_ci#define AVCODEC_TAK_H
29cabdff1aSopenharmony_ci
30cabdff1aSopenharmony_ci#include <stdint.h>
31cabdff1aSopenharmony_ci
32cabdff1aSopenharmony_ci#include "avcodec.h"
33cabdff1aSopenharmony_ci#include "get_bits.h"
34cabdff1aSopenharmony_ci
35cabdff1aSopenharmony_ci#define TAK_FORMAT_DATA_TYPE_BITS               3
36cabdff1aSopenharmony_ci#define TAK_FORMAT_SAMPLE_RATE_BITS            18
37cabdff1aSopenharmony_ci#define TAK_FORMAT_BPS_BITS                     5
38cabdff1aSopenharmony_ci#define TAK_FORMAT_CHANNEL_BITS                 4
39cabdff1aSopenharmony_ci#define TAK_FORMAT_VALID_BITS                   5
40cabdff1aSopenharmony_ci#define TAK_FORMAT_CH_LAYOUT_BITS               6
41cabdff1aSopenharmony_ci#define TAK_SIZE_FRAME_DURATION_BITS            4
42cabdff1aSopenharmony_ci#define TAK_SIZE_SAMPLES_NUM_BITS              35
43cabdff1aSopenharmony_ci#define TAK_LAST_FRAME_POS_BITS                40
44cabdff1aSopenharmony_ci#define TAK_LAST_FRAME_SIZE_BITS               24
45cabdff1aSopenharmony_ci#define TAK_ENCODER_CODEC_BITS                  6
46cabdff1aSopenharmony_ci#define TAK_ENCODER_PROFILE_BITS                4
47cabdff1aSopenharmony_ci#define TAK_SAMPLE_RATE_MIN                  6000
48cabdff1aSopenharmony_ci#define TAK_CHANNELS_MIN                        1
49cabdff1aSopenharmony_ci#define TAK_BPS_MIN                             8
50cabdff1aSopenharmony_ci#define TAK_FRAME_HEADER_FLAGS_BITS             3
51cabdff1aSopenharmony_ci#define TAK_FRAME_HEADER_SYNC_ID           0xA0FF
52cabdff1aSopenharmony_ci#define TAK_FRAME_HEADER_SYNC_ID_BITS          16
53cabdff1aSopenharmony_ci#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS     14
54cabdff1aSopenharmony_ci#define TAK_FRAME_HEADER_NO_BITS               21
55cabdff1aSopenharmony_ci#define TAK_FRAME_DURATION_QUANT_SHIFT          5
56cabdff1aSopenharmony_ci#define TAK_CRC24_BITS                         24
57cabdff1aSopenharmony_ci
58cabdff1aSopenharmony_ci
59cabdff1aSopenharmony_ci#define TAK_FRAME_FLAG_IS_LAST                0x1
60cabdff1aSopenharmony_ci#define TAK_FRAME_FLAG_HAS_INFO               0x2
61cabdff1aSopenharmony_ci#define TAK_FRAME_FLAG_HAS_METADATA           0x4
62cabdff1aSopenharmony_ci
63cabdff1aSopenharmony_ci#define TAK_MAX_CHANNELS               (1 << TAK_FORMAT_CHANNEL_BITS)
64cabdff1aSopenharmony_ci
65cabdff1aSopenharmony_ci#define TAK_MIN_FRAME_HEADER_BITS      (TAK_FRAME_HEADER_SYNC_ID_BITS + \
66cabdff1aSopenharmony_ci                                        TAK_FRAME_HEADER_FLAGS_BITS   + \
67cabdff1aSopenharmony_ci                                        TAK_FRAME_HEADER_NO_BITS      + \
68cabdff1aSopenharmony_ci                                        TAK_CRC24_BITS)
69cabdff1aSopenharmony_ci
70cabdff1aSopenharmony_ci#define TAK_MIN_FRAME_HEADER_LAST_BITS (TAK_MIN_FRAME_HEADER_BITS + 2 + \
71cabdff1aSopenharmony_ci                                        TAK_FRAME_HEADER_SAMPLE_COUNT_BITS)
72cabdff1aSopenharmony_ci
73cabdff1aSopenharmony_ci#define TAK_ENCODER_BITS               (TAK_ENCODER_CODEC_BITS + \
74cabdff1aSopenharmony_ci                                        TAK_ENCODER_PROFILE_BITS)
75cabdff1aSopenharmony_ci
76cabdff1aSopenharmony_ci#define TAK_SIZE_BITS                  (TAK_SIZE_SAMPLES_NUM_BITS + \
77cabdff1aSopenharmony_ci                                        TAK_SIZE_FRAME_DURATION_BITS)
78cabdff1aSopenharmony_ci
79cabdff1aSopenharmony_ci#define TAK_FORMAT_BITS                (TAK_FORMAT_DATA_TYPE_BITS   + \
80cabdff1aSopenharmony_ci                                        TAK_FORMAT_SAMPLE_RATE_BITS + \
81cabdff1aSopenharmony_ci                                        TAK_FORMAT_BPS_BITS         + \
82cabdff1aSopenharmony_ci                                        TAK_FORMAT_CHANNEL_BITS + 1 + \
83cabdff1aSopenharmony_ci                                        TAK_FORMAT_VALID_BITS   + 1 + \
84cabdff1aSopenharmony_ci                                        TAK_FORMAT_CH_LAYOUT_BITS   * \
85cabdff1aSopenharmony_ci                                        TAK_MAX_CHANNELS)
86cabdff1aSopenharmony_ci
87cabdff1aSopenharmony_ci#define TAK_STREAMINFO_BITS            (TAK_ENCODER_BITS + \
88cabdff1aSopenharmony_ci                                        TAK_SIZE_BITS    + \
89cabdff1aSopenharmony_ci                                        TAK_FORMAT_BITS)
90cabdff1aSopenharmony_ci
91cabdff1aSopenharmony_ci#define TAK_MAX_FRAME_HEADER_BITS      (TAK_MIN_FRAME_HEADER_LAST_BITS + \
92cabdff1aSopenharmony_ci                                        TAK_STREAMINFO_BITS + 31)
93cabdff1aSopenharmony_ci
94cabdff1aSopenharmony_ci#define TAK_STREAMINFO_BYTES           ((TAK_STREAMINFO_BITS       + 7) / 8)
95cabdff1aSopenharmony_ci#define TAK_MAX_FRAME_HEADER_BYTES     ((TAK_MAX_FRAME_HEADER_BITS + 7) / 8)
96cabdff1aSopenharmony_ci#define TAK_MIN_FRAME_HEADER_BYTES     ((TAK_MIN_FRAME_HEADER_BITS + 7) / 8)
97cabdff1aSopenharmony_ci
98cabdff1aSopenharmony_cienum TAKCodecType {
99cabdff1aSopenharmony_ci    TAK_CODEC_MONO_STEREO  = 2,
100cabdff1aSopenharmony_ci    TAK_CODEC_MULTICHANNEL = 4,
101cabdff1aSopenharmony_ci};
102cabdff1aSopenharmony_ci
103cabdff1aSopenharmony_cienum TAKMetaDataType {
104cabdff1aSopenharmony_ci    TAK_METADATA_END = 0,
105cabdff1aSopenharmony_ci    TAK_METADATA_STREAMINFO,
106cabdff1aSopenharmony_ci    TAK_METADATA_SEEKTABLE,
107cabdff1aSopenharmony_ci    TAK_METADATA_SIMPLE_WAVE_DATA,
108cabdff1aSopenharmony_ci    TAK_METADATA_ENCODER,
109cabdff1aSopenharmony_ci    TAK_METADATA_PADDING,
110cabdff1aSopenharmony_ci    TAK_METADATA_MD5,
111cabdff1aSopenharmony_ci    TAK_METADATA_LAST_FRAME,
112cabdff1aSopenharmony_ci};
113cabdff1aSopenharmony_ci
114cabdff1aSopenharmony_cienum TAKFrameSizeType {
115cabdff1aSopenharmony_ci    TAK_FST_94ms = 0,
116cabdff1aSopenharmony_ci    TAK_FST_125ms,
117cabdff1aSopenharmony_ci    TAK_FST_188ms,
118cabdff1aSopenharmony_ci    TAK_FST_250ms,
119cabdff1aSopenharmony_ci    TAK_FST_4096,
120cabdff1aSopenharmony_ci    TAK_FST_8192,
121cabdff1aSopenharmony_ci    TAK_FST_16384,
122cabdff1aSopenharmony_ci    TAK_FST_512,
123cabdff1aSopenharmony_ci    TAK_FST_1024,
124cabdff1aSopenharmony_ci    TAK_FST_2048,
125cabdff1aSopenharmony_ci};
126cabdff1aSopenharmony_ci
127cabdff1aSopenharmony_citypedef struct TAKStreamInfo {
128cabdff1aSopenharmony_ci    int               flags;
129cabdff1aSopenharmony_ci    enum TAKCodecType codec;
130cabdff1aSopenharmony_ci    int               data_type;
131cabdff1aSopenharmony_ci    int               sample_rate;
132cabdff1aSopenharmony_ci    int               channels;
133cabdff1aSopenharmony_ci    int               bps;
134cabdff1aSopenharmony_ci    int               frame_num;
135cabdff1aSopenharmony_ci    int               frame_samples;
136cabdff1aSopenharmony_ci    int               last_frame_samples;
137cabdff1aSopenharmony_ci    uint64_t          ch_layout;
138cabdff1aSopenharmony_ci    int64_t           samples;
139cabdff1aSopenharmony_ci} TAKStreamInfo;
140cabdff1aSopenharmony_ci
141cabdff1aSopenharmony_ciint ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size);
142cabdff1aSopenharmony_ci
143cabdff1aSopenharmony_ci/**
144cabdff1aSopenharmony_ci * Parse the Streaminfo metadata block.
145cabdff1aSopenharmony_ci * @param[out] s  storage for parsed information
146cabdff1aSopenharmony_ci * @param[in]  buf   input buffer
147cabdff1aSopenharmony_ci * @param[in]  size  size of input buffer in bytes
148cabdff1aSopenharmony_ci * @return non-zero on error, 0 if OK
149cabdff1aSopenharmony_ci */
150cabdff1aSopenharmony_ciint avpriv_tak_parse_streaminfo(TAKStreamInfo *s, const uint8_t *buf, int size);
151cabdff1aSopenharmony_ci
152cabdff1aSopenharmony_civoid ff_tak_parse_streaminfo(TAKStreamInfo *s, GetBitContext *gb);
153cabdff1aSopenharmony_ci
154cabdff1aSopenharmony_ci/**
155cabdff1aSopenharmony_ci * Validate and decode a frame header.
156cabdff1aSopenharmony_ci * @param      avctx             AVCodecContext to use as av_log() context
157cabdff1aSopenharmony_ci * @param[in]  gb                GetBitContext from which to read frame header
158cabdff1aSopenharmony_ci * @param[out] s                 frame information
159cabdff1aSopenharmony_ci * @param      log_level_offset  log level offset, can be used to silence
160cabdff1aSopenharmony_ci *                               error messages.
161cabdff1aSopenharmony_ci * @return non-zero on error, 0 if OK
162cabdff1aSopenharmony_ci */
163cabdff1aSopenharmony_ciint ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
164cabdff1aSopenharmony_ci                               TAKStreamInfo *s, int log_level_offset);
165cabdff1aSopenharmony_ci#endif /* AVCODEC_TAK_H */
166