libavformat/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 "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))
36  return AVPROBE_SCORE_MAX / 2;
37  return 0;
38 }
39 
41 {
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 (!url_feof(pb)) {
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 {
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 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int channels
Definition: tak.h:134
struct TAKDemuxContext TAKDemuxContext
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.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define tc
Definition: regdef.h:69
uint64_t ch_layout
Definition: tak.h:139
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
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:944
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
enum AVStreamParseType need_parsing
Definition: avformat.h:811
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
Definition: error.h:55
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.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:586
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition: get_bits.h:326
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:114
int size
uint64_t channel_layout
Audio channel layout.
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
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...
#define AV_LOG_VERBOSE
Definition: log.h:157
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:58
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
AVInputFormat ff_tak_demuxer
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
#define TAK_LAST_FRAME_SIZE_BITS
Definition: tak.h:45
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
Parse the Streaminfo metadata block.
Definition: tak.c:108
static int tak_probe(AVProbeData *p)
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
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:73
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:353
synthesis window for stochastic i
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:379
static int tak_read_header(AVFormatContext *s)
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:306
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
int sample_rate
Definition: tak.h:133
static int flags
Definition: cpu.c:23
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define TAK_ENCODER_VERSION_BITS
Definition: tak.h:48
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:571
#define TAK_LAST_FRAME_POS_BITS
Definition: tak.h:44
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.