xref: /third_party/ffmpeg/libavformat/binka.c (revision cabdff1a)
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
21#include "libavutil/intreadwrite.h"
22#include "avformat.h"
23#include "internal.h"
24
25static int binka_probe(const AVProbeData *p)
26{
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;
30    return 0;
31}
32
33static int binka_read_header(AVFormatContext *s)
34{
35    AVIOContext *pb = s->pb;
36    AVStream *st;
37    int entries, offset;
38
39    st = avformat_new_stream(s, NULL);
40    if (!st)
41        return AVERROR(ENOMEM);
42
43    avio_skip(pb, 5);
44
45    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
46    st->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_DCT;
47    st->codecpar->ch_layout.nb_channels = avio_r8(pb);
48    st->codecpar->sample_rate = avio_rl16(pb);
49    st->duration = avio_rl32(pb);
50
51    avio_skip(pb, 8);
52    entries = avio_rl16(pb);
53
54    offset = entries * 2 + 2;
55    avio_skip(pb, offset);
56
57    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
58
59    return 0;
60}
61
62static int binka_read_packet(AVFormatContext *s, AVPacket *pkt)
63{
64    AVIOContext *pb = s->pb;
65    AVStream *st = s->streams[0];
66    int64_t pos;
67    int pkt_size;
68    int ret;
69
70    if (avio_feof(pb))
71        return AVERROR_EOF;
72
73    pos = avio_tell(pb);
74    avio_skip(pb, 2);
75    pkt_size = avio_rl16(pb) + 4;
76    if (pkt_size <= 4)
77        return AVERROR(EIO);
78    ret = av_new_packet(pkt, pkt_size);
79    if (ret < 0)
80        return ret;
81
82    avio_read(pb, pkt->data + 4, pkt_size - 4);
83    AV_WL32(pkt->data, pkt_size);
84
85    pkt->pos = pos;
86    pkt->stream_index = 0;
87    pkt->duration = av_get_audio_frame_duration2(st->codecpar, 0);
88
89    return 0;
90}
91
92const AVInputFormat ff_binka_demuxer = {
93    .name           = "binka",
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",
100};
101