FFmpeg
Loading...
Searching...
No Matches
binka.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
22#include "avformat.h"
23#include "avio_internal.h"
24#include "demux.h"
25#include "internal.h"
26
30
31static int binka_probe(const AVProbeData *p)
32{
33 if (AV_RB32(p->buf) == MKBETAG('1', 'F', 'C', 'B') &&
34 (p->buf[4] == 1 || p->buf[4] == 2))
35 return AVPROBE_SCORE_MAX;
36 if (AV_RL32(p->buf) == MKBETAG('U','E','B','A') && p->buf[4] == 1 &&
37 (p->buf[5] == 1 || p->buf[5] == 2) && AV_RL32(p->buf + 8))
38 return AVPROBE_SCORE_MAX;
39 return 0;
40}
41
43{
44 BinkaDemuxContext *binka = s->priv_data;
45 AVIOContext *pb = s->pb;
46 AVStream *st;
47 int entries, offset;
48
50 if (!st)
51 return AVERROR(ENOMEM);
52
53 binka->is_ueba = avio_rl32(pb) == MKBETAG('U','E','B','A');
54
55 avio_skip(pb, 1);
56
60 if (binka->is_ueba) {
61 avio_skip(pb, 2);
63 if (st->codecpar->sample_rate <= 0)
65 } else
67 st->duration = avio_rl32(pb);
68
69 avio_skip(pb, 8);
70 entries = avio_rl16(pb);
71
72 offset = entries * 2 + 2;
73 avio_skip(pb, offset);
74
76
77 if (binka->is_ueba) {
78 int ret = ff_alloc_extradata(st->codecpar, 1);
79 if (ret < 0)
80 return ret;
81 st->codecpar->extradata[0] = '2';
82 }
83
84 return 0;
85}
86
88{
89 BinkaDemuxContext *binka = s->priv_data;
90 AVIOContext *pb = s->pb;
91 AVStream *st = s->streams[0];
93 int pkt_size;
94 int discard_padding = 0;
95 int ret;
96
97 pos = avio_tell(pb);
98 avio_skip(pb, 2);
99
100 if (avio_feof(pb))
101 return AVERROR_EOF;
102
103 pkt_size = avio_rl16(pb);
104 if (!pkt_size)
105 return AVERROR_INVALIDDATA;
106
107 if (binka->is_ueba && pkt_size == 0xFFFF) {
108 pkt_size = avio_rl16(pb);
109 duration = avio_rl16(pb);
110 if (duration > 0) {
111 /* Match the decoded DCT block size, excluding overlap. */
112 int frame_samples = st->codecpar->sample_rate < 22050 ? 480 :
113 st->codecpar->sample_rate < 44100 ? 960 : 1920;
114 discard_padding = FFMAX(0, frame_samples - duration);
115 }
116 } else
118
119 ret = av_new_packet(pkt, pkt_size + 4);
120 if (ret < 0)
121 return ret;
122
123 ret = ffio_read_size(pb, pkt->data + 4, pkt_size);
124 if (ret < 0)
125 return ret;
126 AV_WL32(pkt->data, pkt_size);
127
128 if (discard_padding) {
129 uint8_t *side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
130 if (!side_data)
131 return AVERROR(ENOMEM);
132 AV_WL32(side_data + 4, discard_padding);
133 }
134
135 pkt->pos = pos;
136 pkt->stream_index = 0;
137 pkt->duration = duration;
138
139 return 0;
140}
141
143 .p.name = "binka",
144 .p.long_name = NULL_IF_CONFIG_SMALL("Bink Audio"),
146 .p.extensions = "binka",
147 .priv_data_size = sizeof(BinkaDemuxContext),
151};
const FFInputFormat ff_binka_demuxer
Definition binka.c:142
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:839
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:501
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:500
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
unsigned int avio_rl16(AVIOContext *s)
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)
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
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 binka_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition binka.c:87
static int binka_probe(const AVProbeData *p)
Definition binka.c:31
static int binka_read_header(AVFormatContext *s)
Definition binka.c:42
#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
static AVPacket * pkt
static int64_t duration
Definition ffplay.c:399
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_BINKAUDIO_DCT
Definition codec_id.h:503
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#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
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RB32(p)
unsigned offset
Definition libaomenc.c:763
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
Definition utils.c:824
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFMAX(a, b)
Definition macros.h:47
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
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:1337
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
Definition sync_queue.c:131