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 #define BITSTREAM_READER_LE
23 #include "libavcodec/tak.h"
24 
25 #include "apetag.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "rawdec.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 
43 {
44  TAKDemuxContext *tc = s->priv_data;
45  AVIOContext *pb = s->pb;
46  GetBitContext gb;
47  AVStream *st;
48  uint8_t *buffer = NULL;
49  int ret;
50 
51  st = avformat_new_stream(s, 0);
52  if (!st)
53  return AVERROR(ENOMEM);
54 
58 
59  tc->mlast_frame = 0;
60  if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
61  avio_seek(pb, -4, SEEK_CUR);
62  return 0;
63  }
64 
65  while (!pb->eof_reached) {
66  enum TAKMetaDataType type;
67  int size;
68 
69  type = avio_r8(pb) & 0x7f;
70  size = avio_rl24(pb);
71 
72  switch (type) {
76  buffer = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
77  if (!buffer)
78  return AVERROR(ENOMEM);
79 
80  if (avio_read(pb, buffer, size) != size) {
81  av_freep(&buffer);
82  return AVERROR(EIO);
83  }
84 
85  init_get_bits(&gb, buffer, size * 8);
86  break;
87  case TAK_METADATA_MD5: {
88  uint8_t md5[16];
89  int i;
90 
91  if (size != 19)
92  return AVERROR_INVALIDDATA;
93  avio_read(pb, md5, 16);
94  avio_skip(pb, 3);
95  av_log(s, AV_LOG_VERBOSE, "MD5=");
96  for (i = 0; i < 16; i++)
97  av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
98  av_log(s, AV_LOG_VERBOSE, "\n");
99  break;
100  }
101  case TAK_METADATA_END: {
102  int64_t curpos = avio_tell(pb);
103 
104  if (pb->seekable) {
105  ff_ape_parse_tag(s);
106  avio_seek(pb, curpos, SEEK_SET);
107  }
108 
109  tc->data_end += curpos;
110  return 0;
111  }
112  default:
113  ret = avio_skip(pb, size);
114  if (ret < 0)
115  return ret;
116  }
117 
118  if (type == TAK_METADATA_STREAMINFO) {
119  TAKStreamInfo ti;
120 
121  avpriv_tak_parse_streaminfo(&gb, &ti);
122  if (ti.samples > 0)
123  st->duration = ti.samples;
125  if (ti.ch_layout)
127  st->codecpar->sample_rate = ti.sample_rate;
128  st->codecpar->channels = ti.channels;
129  st->start_time = 0;
130  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
131  st->codecpar->extradata = buffer;
133  buffer = NULL;
134  } else if (type == TAK_METADATA_LAST_FRAME) {
135  if (size != 11)
136  return AVERROR_INVALIDDATA;
137  tc->mlast_frame = 1;
140  av_freep(&buffer);
141  } else if (type == TAK_METADATA_ENCODER) {
142  av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
144  av_freep(&buffer);
145  }
146  }
147 
148  return AVERROR_EOF;
149 }
150 
152 {
153  TAKDemuxContext *tc = s->priv_data;
154  int ret;
155 
156  if (tc->mlast_frame) {
157  AVIOContext *pb = s->pb;
158  int64_t size, left;
159 
160  left = tc->data_end - avio_tell(s->pb);
161  size = FFMIN(left, 1024);
162  if (size <= 0)
163  return AVERROR_EOF;
164 
165  ret = av_get_packet(pb, pkt, size);
166  if (ret < 0)
167  return ret;
168 
169  pkt->stream_index = 0;
170  } else {
171  ret = ff_raw_read_partial_packet(s, pkt);
172  }
173 
174  return ret;
175 }
176 
178  .name = "tak",
179  .long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
180  .priv_data_size = sizeof(TAKDemuxContext),
185  .extensions = "tak",
186  .raw_codec_id = AV_CODEC_ID_TAK,
187 };
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:104
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
int channels
Definition: tak.h:133
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
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:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
uint64_t ch_layout
Definition: tak.h:138
TAKMetaDataType
Definition: tak.h:104
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:202
Format I/O context.
Definition: avformat.h:940
static char buffer[20]
Definition: seek.c:32
uint8_t
enum AVStreamParseType need_parsing
Definition: avformat.h:879
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
int64_t data_end
Definition: takdec.c:32
struct AVMD5 * md5
Definition: movenc.c:56
static int flags
Definition: log.c:50
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
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:295
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
static uint64_t get_bits64(GetBitContext *s, int n)
Definition: get_bits.h:318
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#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:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: takdec.c:151
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:123
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
int bps
Definition: tak.h:134
int64_t samples
Definition: tak.h:139
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
#define FFMIN(a, b)
Definition: common.h:66
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:286
AVInputFormat ff_tak_demuxer
Definition: takdec.c:177
#define TAK_LAST_FRAME_SIZE_BITS
Definition: tak.h:44
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
Parse the Streaminfo metadata block.
Definition: tak.c:88
static int tak_probe(AVProbeData *p)
Definition: takdec.c:35
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:421
TAK (Tom&#39;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:362
int mlast_frame
Definition: takdec.c:31
static int tak_read_header(AVFormatContext *s)
Definition: takdec.c:42
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:405
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:300
int sample_rate
Definition: tak.h:132
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
int sample_rate
Audio only.
Definition: avcodec.h:3564
full parsing and repack
Definition: avformat.h:657
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:750
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
int eof_reached
true if eof reached
Definition: avio.h:132
void * priv_data
Format private data.
Definition: avformat.h:968
int bits_per_coded_sample
Definition: avcodec.h:3514
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int channels
Audio only.
Definition: avcodec.h:3560
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
#define TAK_ENCODER_VERSION_BITS
Definition: tak.h:47
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:657
int stream_index
Definition: avcodec.h:1348
#define TAK_LAST_FRAME_POS_BITS
Definition: tak.h:43
#define MKTAG(a, b, c, d)
Definition: common.h:256
This structure stores compressed data.
Definition: avcodec.h:1323