/src/ffmpeg/libavformat/qoadec.c
Line | Count | Source |
1 | | /* |
2 | | * QOA demuxer |
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 "avformat.h" |
22 | | #include "avio_internal.h" |
23 | | #include "demux.h" |
24 | | #include "internal.h" |
25 | | #include "libavutil/intreadwrite.h" |
26 | | |
27 | | static int qoa_probe(const AVProbeData *p) |
28 | 971k | { |
29 | 971k | if ((p->buf_size < 16) || |
30 | 823k | (AV_RB32(p->buf) != MKBETAG('q','o','a','f')) || |
31 | 1.67k | (AV_RB32(p->buf + 4) == 0) || |
32 | 1.45k | (p->buf[8] == 0) || |
33 | 1.30k | (AV_RB24(p->buf + 9) == 0) || |
34 | 1.00k | (AV_RB16(p->buf + 12) == 0) || |
35 | 611 | (AV_RB16(p->buf + 14) == 0)) |
36 | 971k | return 0; |
37 | 415 | return AVPROBE_SCORE_MAX; |
38 | 971k | } |
39 | | |
40 | | static int qoa_read_header(AVFormatContext *s) |
41 | 1.36k | { |
42 | 1.36k | AVIOContext *pb = s->pb; |
43 | 1.36k | AVStream *st; |
44 | 1.36k | int ret; |
45 | | |
46 | 1.36k | st = avformat_new_stream(s, NULL); |
47 | 1.36k | if (!st) |
48 | 0 | return AVERROR(ENOMEM); |
49 | | |
50 | 1.36k | avio_skip(pb, 4); |
51 | 1.36k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
52 | 1.36k | st->codecpar->codec_id = AV_CODEC_ID_QOA; |
53 | 1.36k | st->duration = avio_rb32(pb); |
54 | 1.36k | st->start_time = 0; |
55 | | |
56 | 1.36k | ret = ffio_ensure_seekback(pb, 4); |
57 | 1.36k | if (ret < 0) |
58 | 0 | return ret; |
59 | 1.36k | st->codecpar->ch_layout.nb_channels = avio_r8(pb); |
60 | 1.36k | if (st->codecpar->ch_layout.nb_channels == 0) |
61 | 149 | return AVERROR_INVALIDDATA; |
62 | | |
63 | 1.21k | st->codecpar->sample_rate = avio_rb24(pb); |
64 | 1.21k | if (st->codecpar->sample_rate == 0) |
65 | 12 | return AVERROR_INVALIDDATA; |
66 | | |
67 | 1.20k | avio_seek(pb, -4, SEEK_CUR); |
68 | | |
69 | 1.20k | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); |
70 | | |
71 | 1.20k | return 0; |
72 | 1.21k | } |
73 | | |
74 | | static int qoa_read_packet(AVFormatContext *s, AVPacket *pkt) |
75 | 154k | { |
76 | 154k | AVIOContext *pb = s->pb; |
77 | 154k | uint16_t size, duration; |
78 | 154k | uint8_t hdr[8]; |
79 | 154k | int64_t pos; |
80 | 154k | int ret; |
81 | | |
82 | 154k | if (avio_feof(pb)) |
83 | 930 | return AVERROR_EOF; |
84 | | |
85 | 153k | pos = avio_tell(pb); |
86 | 153k | ret = avio_read(pb, hdr, sizeof(hdr)); |
87 | 153k | if (ret != sizeof(hdr)) |
88 | 773 | return AVERROR_EOF; |
89 | | |
90 | 152k | duration = AV_RB16(hdr + 4); |
91 | 152k | size = AV_RB16(hdr + 6); |
92 | 152k | if ((ret = av_new_packet(pkt, size)) < 0) |
93 | 0 | return ret; |
94 | | |
95 | 152k | memcpy(pkt->data, hdr, sizeof(hdr)); |
96 | 152k | ret = ffio_read_size(pb, pkt->data + sizeof(hdr), size - sizeof(hdr)); |
97 | 152k | if (ret < 0) |
98 | 412 | return ret; |
99 | 151k | pkt->stream_index = 0; |
100 | 151k | pkt->pos = pos; |
101 | 151k | pkt->duration = duration; |
102 | | |
103 | 151k | return 0; |
104 | 152k | } |
105 | | |
106 | | const FFInputFormat ff_qoa_demuxer = { |
107 | | .p.name = "qoa", |
108 | | .p.long_name = NULL_IF_CONFIG_SMALL("QOA"), |
109 | | .p.flags = AVFMT_GENERIC_INDEX, |
110 | | .p.extensions = "qoa", |
111 | | .read_probe = qoa_probe, |
112 | | .read_header = qoa_read_header, |
113 | | .read_packet = qoa_read_packet, |
114 | | }; |