FFmpeg  2.1.1
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 
22 #include "libavutil/crc.h"
23 #include "libavcodec/tak.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "internal.h"
27 #include "rawdec.h"
28 #include "apetag.h"
29 
30 typedef struct TAKDemuxContext {
32  int64_t data_end;
34 
35 static int tak_probe(AVProbeData *p)
36 {
37  if (!memcmp(p->buf, "tBaK", 4))
39  return 0;
40 }
41 
42 static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf,
43  unsigned int len)
44 {
45  return av_crc(av_crc_get_table(AV_CRC_24_IEEE), checksum, buf, len);
46 }
47 
49 {
51  AVIOContext *pb = s->pb;
52  GetBitContext gb;
53  AVStream *st;
54  uint8_t *buffer = NULL;
55  int ret;
56 
57  st = avformat_new_stream(s, 0);
58  if (!st)
59  return AVERROR(ENOMEM);
60 
64 
65  tc->mlast_frame = 0;
66  if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
67  avio_seek(pb, -4, SEEK_CUR);
68  return 0;
69  }
70 
71  while (!url_feof(pb)) {
72  enum TAKMetaDataType type;
73  int size;
74 
75  type = avio_r8(pb) & 0x7f;
76  size = avio_rl24(pb);
77 
78  switch (type) {
82  if (size <= 3)
83  return AVERROR_INVALIDDATA;
84 
85  buffer = av_malloc(size - 3 + FF_INPUT_BUFFER_PADDING_SIZE);
86  if (!buffer)
87  return AVERROR(ENOMEM);
88 
89  ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
90  if (avio_read(pb, buffer, size - 3) != size - 3) {
91  av_freep(&buffer);
92  return AVERROR(EIO);
93  }
94  if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
95  av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type);
97  av_freep(&buffer);
98  return AVERROR_INVALIDDATA;
99  }
100  }
101 
102  init_get_bits8(&gb, buffer, size - 3);
103  break;
104  case TAK_METADATA_MD5: {
105  uint8_t md5[16];
106  int i;
107 
108  if (size != 19)
109  return AVERROR_INVALIDDATA;
110  ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
111  avio_read(pb, md5, 16);
112  if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
113  av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n");
115  return AVERROR_INVALIDDATA;
116  }
117 
118  av_log(s, AV_LOG_VERBOSE, "MD5=");
119  for (i = 0; i < 16; i++)
120  av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
121  av_log(s, AV_LOG_VERBOSE, "\n");
122  break;
123  }
124  case TAK_METADATA_END: {
125  int64_t curpos = avio_tell(pb);
126 
127  if (pb->seekable) {
128  ff_ape_parse_tag(s);
129  avio_seek(pb, curpos, SEEK_SET);
130  }
131 
132  tc->data_end += curpos;
133  return 0;
134  }
135  default:
136  ret = avio_skip(pb, size);
137  if (ret < 0)
138  return ret;
139  }
140 
141  if (type == TAK_METADATA_STREAMINFO) {
142  TAKStreamInfo ti;
143 
144  avpriv_tak_parse_streaminfo(&gb, &ti);
145  if (ti.samples > 0)
146  st->duration = ti.samples;
147  st->codec->bits_per_coded_sample = ti.bps;
148  if (ti.ch_layout)
149  st->codec->channel_layout = ti.ch_layout;
150  st->codec->sample_rate = ti.sample_rate;
151  st->codec->channels = ti.channels;
152  st->start_time = 0;
153  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
154  st->codec->extradata = buffer;
155  st->codec->extradata_size = size - 3;
156  buffer = NULL;
157  } else if (type == TAK_METADATA_LAST_FRAME) {
158  if (size != 11)
159  return AVERROR_INVALIDDATA;
160  tc->mlast_frame = 1;
163  av_freep(&buffer);
164  } else if (type == TAK_METADATA_ENCODER) {
165  av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
167  av_freep(&buffer);
168  }
169  }
170 
171  return AVERROR_EOF;
172 }
173 
175 {
177  int ret;
178 
179  if (tc->mlast_frame) {
180  AVIOContext *pb = s->pb;
181  int64_t size, left;
182 
183  left = tc->data_end - avio_tell(pb);
184  size = FFMIN(left, 1024);
185  if (size <= 0)
186  return AVERROR_EOF;
187 
188  ret = av_get_packet(pb, pkt, size);
189  if (ret < 0)
190  return ret;
191 
192  pkt->stream_index = 0;
193  } else {
194  ret = ff_raw_read_partial_packet(s, pkt);
195  }
196 
197  return ret;
198 }
199 
201  .name = "tak",
202  .long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
203  .priv_data_size = sizeof(TAKDemuxContext),
208  .extensions = "tak",
209  .raw_codec_id = AV_CODEC_ID_TAK,
210 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
int size
int channels
Definition: tak.h:134
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:306
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3922
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:593
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define tc
Definition: regdef.h:69
uint64_t ch_layout
Definition: tak.h:139
TAKMetaDataType
Definition: tak.h:105
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:471
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
Format I/O context.
Definition: avformat.h:968
uint8_t
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
enum AVStreamParseType need_parsing
Definition: avformat.h:812
int64_t data_end
Definition: takdec.c:32
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
#define AV_LOG_VERBOSE
Detailed information.
Definition: avcodec.h:4163
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:617
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:200
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: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:34
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: takdec.c:174
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:115
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:355
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1934
int bps
Definition: tak.h:135
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
int64_t samples
Definition: tak.h:140
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:459
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
ret
Definition: avfilter.c:961
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:585
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:341
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length) av_pure
Calculate the CRC of a block.
Definition: crc.c:320
#define FFMIN(a, b)
Definition: avcodec.h:925
AVInputFormat ff_tak_demuxer
Definition: takdec.c:200
static char buffer[20]
Definition: seek-test.c:31
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define TAK_LAST_FRAME_SIZE_BITS
Definition: tak.h:45
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
int sample_rate
samples per second
Definition: avcodec.h:1873
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:436
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
Parse the Streaminfo metadata block.
Definition: tak.c:91
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2480
static int tak_probe(AVProbeData *p)
Definition: takdec.c:35
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
void * buf
Definition: avisynth_c.h:594
int extradata_size
Definition: avcodec.h:1255
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define MKTAG(a, b, c, d)
TAK (Tom&#39;s lossless Audio Kompressor) decoder/demuxer common functions.
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:451
int mlast_frame
Definition: takdec.c:31
static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: takdec.c:42
static int tak_read_header(AVFormatContext *s)
Definition: takdec.c:48
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:332
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define type
int sample_rate
Definition: tak.h:133
static int flags
Definition: cpu.c:45
#define AVERROR_EOF
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:720
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:480
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:713
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1150
Main libavformat public API header.
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:603
#define AVERROR_INVALIDDATA
int len
int channels
number of audio channels
Definition: avcodec.h:1874
#define AVERROR(e)
#define TAK_ENCODER_VERSION_BITS
Definition: tak.h:48
static AVPacket pkt
Definition: demuxing.c:52
int stream_index
Definition: avcodec.h:1065
#define TAK_LAST_FRAME_POS_BITS
Definition: tak.h:44
This structure stores compressed data.
Definition: avcodec.h:1040