2 * Copyright (c) 2021 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/intreadwrite.h"
25 static int binka_probe(const AVProbeData *p)
27 if (AV_RB32(p->buf) == MKBETAG('1', 'F', 'C', 'B') &&
28 (p->buf[4] == 1 || p->buf[4] == 2))
29 return AVPROBE_SCORE_MAX;
33 static int binka_read_header(AVFormatContext *s)
35 AVIOContext *pb = s->pb;
39 st = avformat_new_stream(s, NULL);
41 return AVERROR(ENOMEM);
45 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
46 st->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_DCT;
47 st->codecpar->channels = avio_r8(pb);
48 st->codecpar->sample_rate = avio_rl16(pb);
49 st->duration = avio_rl32(pb);
52 entries = avio_rl16(pb);
54 offset = entries * 2 + 2;
55 avio_skip(pb, offset);
57 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
62 static int binka_read_packet(AVFormatContext *s, AVPacket *pkt)
64 AVIOContext *pb = s->pb;
65 AVStream *st = s->streams[0];
75 pkt_size = avio_rl16(pb) + 4;
78 ret = av_new_packet(pkt, pkt_size);
82 avio_read(pb, pkt->data + 4, pkt_size - 4);
83 AV_WL32(pkt->data, pkt_size);
86 pkt->stream_index = 0;
87 pkt->duration = av_get_audio_frame_duration2(st->codecpar, 0);
92 AVInputFormat ff_binka_demuxer = {
94 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio"),
95 .read_probe = binka_probe,
96 .read_header = binka_read_header,
97 .read_packet = binka_read_packet,
98 .flags = AVFMT_GENERIC_INDEX,
99 .extensions = "binka",