Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavcodec/tak.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "rawdec.h"
26 #include "apetag.h"
27 
28 typedef struct TAKDemuxContext {
30  int64_t data_end;
32 
33 static int tak_probe(AVProbeData *p)
34 {
35  if (!memcmp(p->buf, "tBaK", 4))
37  return 0;
38 }
39 
41 {
42  TAKDemuxContext *tc = s->priv_data;
43  AVIOContext *pb = s->pb;
44  GetBitContext gb;
45  AVStream *st;
46  uint8_t *buffer = NULL;
47  int ret;
48 
49  st = avformat_new_stream(s, 0);
50  if (!st)
51  return AVERROR(ENOMEM);
52 
56 
57  tc->mlast_frame = 0;
58  if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
59  avio_seek(pb, -4, SEEK_CUR);
60  return 0;
61  }
62 
63  while (!pb->eof_reached) {
64  enum TAKMetaDataType type;
65  int size;
66 
67  type = avio_r8(pb) & 0x7f;
68  size = avio_rl24(pb);
69 
70  switch (type) {
74  buffer = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
75  if (!buffer)
76  return AVERROR(ENOMEM);
77 
78  if (avio_read(pb, buffer, size) != size) {
79  av_freep(&buffer);
80  return AVERROR(EIO);
81  }
82 
83  init_get_bits(&gb, buffer, size * 8);
84  break;
85  case TAK_METADATA_MD5: {
86  uint8_t md5[16];
87  int i;
88 
89  if (size != 19)
90  return AVERROR_INVALIDDATA;
91  avio_read(pb, md5, 16);
92  avio_skip(pb, 3);
93  av_log(s, AV_LOG_VERBOSE, "MD5=");
94  for (i = 0; i < 16; i++)
95  av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
96  av_log(s, AV_LOG_VERBOSE, "\n");
97  break;
98  }
99  case TAK_METADATA_END: {
100  int64_t curpos = avio_tell(pb);
101 
102  if (pb->seekable) {
103  ff_ape_parse_tag(s);
104  avio_seek(pb, curpos, SEEK_SET);
105  }
106 
107  tc->data_end += curpos;
108  return 0;
109  }
110  default:
111  ret = avio_skip(pb, size);
112  if (ret < 0)
113  return ret;
114  }
115 
116  if (type == TAK_METADATA_STREAMINFO) {
117  TAKStreamInfo ti;
118 
119  avpriv_tak_parse_streaminfo(&gb, &ti);
120  if (ti.samples > 0)
121  st->duration = ti.samples;
122  st->codec->bits_per_coded_sample = ti.bps;
123  if (ti.ch_layout)
124  st->codec->channel_layout = ti.ch_layout;
125  st->codec->sample_rate = ti.sample_rate;
126  st->codec->channels = ti.channels;
127  st->start_time = 0;
128  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
129  st->codec->extradata = buffer;
130  st->codec->extradata_size = size;
131  buffer = NULL;
132  } else if (type == TAK_METADATA_LAST_FRAME) {
133  if (size != 11)
134  return AVERROR_INVALIDDATA;
135  tc->mlast_frame = 1;
138  av_freep(&buffer);
139  } else if (type == TAK_METADATA_ENCODER) {
140  av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
142  av_freep(&buffer);
143  }
144  }
145 
146  return AVERROR_EOF;
147 }
148 
150 {
151  TAKDemuxContext *tc = s->priv_data;
152  int ret;
153 
154  if (tc->mlast_frame) {
155  AVIOContext *pb = s->pb;
156  int64_t size, left;
157 
158  left = tc->data_end - avio_tell(s->pb);
159  size = FFMIN(left, 1024);
160  if (size <= 0)
161  return AVERROR_EOF;
162 
163  ret = av_get_packet(pb, pkt, size);
164  if (ret < 0)
165  return ret;
166 
167  pkt->stream_index = 0;
168  } else {
169  ret = ff_raw_read_partial_packet(s, pkt);
170  }
171 
172  return ret;
173 }
174 
176  .name = "tak",
177  .long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
178  .priv_data_size = sizeof(TAKDemuxContext),
183  .extensions = "tak",
184  .raw_codec_id = AV_CODEC_ID_TAK,
185 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
int channels
Definition: tak.h:134
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
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:2821
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
uint64_t ch_layout
Definition: tak.h:139
TAKMetaDataType
Definition: tak.h:105
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:922
uint8_t
enum AVStreamParseType need_parsing
Definition: avformat.h:867
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
int64_t data_end
Definition: takdec.c:30
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
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:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2507
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
static uint64_t get_bits64(GetBitContext *s, int n)
Definition: get_bits.h:322
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
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:150
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: takdec.c:149
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:120
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
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:531
int64_t samples
Definition: tak.h:140
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
#define FFMIN(a, b)
Definition: common.h:57
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
AVInputFormat ff_tak_demuxer
Definition: takdec.c:175
static char buffer[20]
Definition: seek-test.c:31
#define TAK_LAST_FRAME_SIZE_BITS
Definition: tak.h:45
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
Parse the Streaminfo metadata block.
Definition: tak.c:86
static int tak_probe(AVProbeData *p)
Definition: takdec.c:33
int extradata_size
Definition: avcodec.h:1165
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:417
TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int mlast_frame
Definition: takdec.c:29
static int tak_read_header(AVFormatContext *s)
Definition: takdec.c:40
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:402
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:304
int sample_rate
Definition: tak.h:133
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:756
full parsing and repack
Definition: avformat.h:653
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1792
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
#define TAK_ENCODER_VERSION_BITS
Definition: tak.h:48
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:556
int stream_index
Definition: avcodec.h:975
#define TAK_LAST_FRAME_POS_BITS
Definition: tak.h:44
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:950