FFmpeg
Loading...
Searching...
No Matches
takdec.c
Go to the documentation of this file.
1/*
2 * Raw TAK demuxer
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/mem.h"
25
26#define BITSTREAM_READER_LE
27#include "libavcodec/tak.h"
28
29#include "apetag.h"
30#include "avformat.h"
31#include "avio_internal.h"
32#include "demux.h"
33#include "internal.h"
34#include "rawdec.h"
35
41
42static int tak_probe(const AVProbeData *p)
43{
44 if (!memcmp(p->buf, "tBaK", 4))
46 return 0;
47}
48
49static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf,
50 unsigned int len)
51{
52 return av_crc(av_crc_get_table(AV_CRC_24_IEEE), checksum, buf, len);
53}
54
56{
57 TAKDemuxContext *tc = s->priv_data;
58 AVIOContext *pb = s->pb;
60 AVStream *st;
61 uint8_t *buffer = NULL;
62 int ret;
63
64 st = avformat_new_stream(s, 0);
65 if (!st)
66 return AVERROR(ENOMEM);
67
71
72 tc->mlast_frame = 0;
73 if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
74 avio_seek(pb, -4, SEEK_CUR);
75 return 0;
76 }
77
78 while (!avio_feof(pb)) {
80 int size;
81
82 type = avio_r8(pb) & 0x7f;
83 size = avio_rl24(pb);
84
85 switch (type) {
87 if (st->codecpar->extradata)
92 if (size <= 3)
94
96 if (!buffer)
97 return AVERROR(ENOMEM);
99
100 ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
101 ret = ffio_read_size(pb, buffer, size - 3);
102 if (ret < 0) {
104 return ret;
105 }
106 if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
107 av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type);
108 if (s->error_recognition & AV_EF_EXPLODE) {
110 return AVERROR_INVALIDDATA;
111 }
112 }
113
114 break;
115 case TAK_METADATA_MD5: {
116 uint8_t md5[16];
117 char md5_hex[2 * sizeof(md5) + 1];
118
119 if (size != 19)
120 return AVERROR_INVALIDDATA;
121 ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
122 ret = ffio_read_size(pb, md5, 16);
123 if (ret < 0)
124 return ret;
125 if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
126 av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n");
127 if (s->error_recognition & AV_EF_EXPLODE)
128 return AVERROR_INVALIDDATA;
129 }
130
131 ff_data_to_hex(md5_hex, md5, sizeof(md5), 1);
132 av_log(s, AV_LOG_VERBOSE, "MD5=%s\n", md5_hex);
133 break;
134 }
135 case TAK_METADATA_END: {
136 int64_t curpos = avio_tell(pb);
137
138 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
140 avio_seek(pb, curpos, SEEK_SET);
141 }
142
143 tc->data_end += curpos;
144 return 0;
145 }
146 default: {
147 int64_t ret64 = avio_skip(pb, size);
148 if (ret64 < 0)
149 return ret64;
150 }
151 }
152
154 TAKStreamInfo ti;
155
157 if (ret < 0)
158 goto end;
159 if (ti.samples > 0)
160 st->duration = ti.samples;
162 if (ti.ch_layout)
164 else {
168 }
170 st->start_time = 0;
173 st->codecpar->extradata_size = size - 3;
174 buffer = NULL;
175 } else if (type == TAK_METADATA_LAST_FRAME) {
176 if (size != 11) {
178 goto end;
179 }
180 init_get_bits8(&gb, buffer, size - 3);
181 tc->mlast_frame = 1;
185 } else if (type == TAK_METADATA_ENCODER) {
186 av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
187 AV_RL24(buffer));
189 }
190 }
191
192 return AVERROR_EOF;
193end:
195 return ret;
196}
197
199{
200 TAKDemuxContext *tc = s->priv_data;
201 int ret;
202
203 if (tc->mlast_frame) {
204 AVIOContext *pb = s->pb;
206
207 left = tc->data_end - avio_tell(pb);
208 size = FFMIN(left, 1024);
209 if (size <= 0)
210 return AVERROR_EOF;
211
212 ret = av_get_packet(pb, pkt, size);
213 if (ret < 0)
214 return ret;
215
216 pkt->stream_index = 0;
217 } else {
219 }
220
221 return ret;
222}
223
225 .p.name = "tak",
226 .p.long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
227 .p.flags = AVFMT_GENERIC_INDEX,
228 .p.extensions = "tak",
229 .p.priv_class = &ff_raw_demuxer_class,
230 .priv_data_size = sizeof(TAKDemuxContext),
234 .raw_codec_id = AV_CODEC_ID_TAK,
235};
const FFInputFormat ff_tak_demuxer
Definition takdec.c:224
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition apetag.c:110
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition avformat.h:615
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rl24(AVIOContext *s)
Definition aviobuf.c:725
unsigned int avio_rb24(AVIOContext *s)
Definition aviobuf.c:757
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition aviobuf.c:594
unsigned long ffio_get_checksum(AVIOContext *s)
Definition aviobuf.c:586
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
static AVPacket * pkt
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition dtshddec.c:158
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition get_bits.h:456
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
@ AV_CODEC_ID_TAK
Definition codec_id.h:515
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
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:421
@ AV_CRC_24_IEEE
Definition crc.h:55
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
cl_device_type type
#define AV_RL24(x)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition utils.c:464
const AVClass ff_raw_demuxer_class
Definition rawdec.c:141
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawdec.c:33
static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition takdec.c:49
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition takdec.c:198
static int tak_probe(const AVProbeData *p)
Definition takdec.c:42
static int tak_read_header(AVFormatContext *s)
Definition takdec.c:55
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
enum AVStreamParseType need_parsing
Definition internal.h:321
int mlast_frame
Definition takdec.c:38
FFRawDemuxerContext rawctx
Definition takdec.c:37
int64_t data_end
Definition takdec.c:39
uint64_t ch_layout
Definition tak.h:136
int64_t samples
Definition tak.h:137
int sample_rate
Definition tak.h:130
int bps
Definition tak.h:132
int channels
Definition tak.h:131
#define av_freep(p)
#define av_log(a,...)
int avpriv_tak_parse_streaminfo(TAKStreamInfo *s, const uint8_t *buf, int size)
Parse the Streaminfo metadata block.
Definition tak.c:136
TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions.
TAKMetaDataType
Definition tak.h:102
@ TAK_METADATA_STREAMINFO
Definition tak.h:104
@ TAK_METADATA_END
Definition tak.h:103
@ TAK_METADATA_MD5
Definition tak.h:109
@ TAK_METADATA_ENCODER
Definition tak.h:107
@ TAK_METADATA_LAST_FRAME
Definition tak.h:110
#define TAK_LAST_FRAME_POS_BITS
Definition tak.h:42
#define TAK_LAST_FRAME_SIZE_BITS
Definition tak.h:43
static struct AVMD5 * md5
Definition movenc.c:57
static char buffer[20]
Definition seek.c:32
int size
int len