/src/ffmpeg/libavformat/ac4dec.c
Line | Count | Source |
1 | | /* |
2 | | * RAW AC-4 demuxer |
3 | | * Copyright (c) 2019 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 "libavutil/avassert.h" |
23 | | #include "libavutil/crc.h" |
24 | | #include "avformat.h" |
25 | | #include "demux.h" |
26 | | #include "rawdec.h" |
27 | | |
28 | | static int ac4_probe(const AVProbeData *p) |
29 | 958k | { |
30 | 958k | const uint8_t *buf = p->buf; |
31 | 958k | int left = p->buf_size; |
32 | 958k | int max_frames = 0; |
33 | | |
34 | 959k | while (left > 7) { |
35 | 874k | int size; |
36 | | |
37 | 874k | if (buf[0] == 0xAC && |
38 | 1.93k | (buf[1] == 0x40 || |
39 | 1.49k | buf[1] == 0x41)) { |
40 | 1.49k | size = (buf[2] << 8) | buf[3]; |
41 | 1.49k | if (size == 0xFFFF) |
42 | 261 | size = 3 + ((buf[4] << 16) | (buf[5] << 8) | buf[6]); |
43 | 1.49k | size += 4; |
44 | 1.49k | if (buf[1] == 0x41) |
45 | 922 | size += 2; |
46 | 1.49k | if (left < size) |
47 | 469 | break; |
48 | 1.02k | max_frames++; |
49 | 1.02k | left -= size; |
50 | 1.02k | buf += size; |
51 | 872k | } else { |
52 | 872k | break; |
53 | 872k | } |
54 | 874k | } |
55 | | |
56 | 958k | return FFMIN(AVPROBE_SCORE_MAX, max_frames * 7); |
57 | 958k | } |
58 | | |
59 | | static int ac4_read_header(AVFormatContext *s) |
60 | 1.24k | { |
61 | 1.24k | AVStream *st; |
62 | | |
63 | 1.24k | st = avformat_new_stream(s, NULL); |
64 | 1.24k | if (!st) |
65 | 0 | return AVERROR(ENOMEM); |
66 | | |
67 | 1.24k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
68 | 1.24k | st->codecpar->codec_id = AV_CODEC_ID_AC4; |
69 | | |
70 | 1.24k | return 0; |
71 | 1.24k | } |
72 | | |
73 | | static int ac4_read_packet(AVFormatContext *s, AVPacket *pkt) |
74 | 563k | { |
75 | 563k | AVIOContext *pb = s->pb; |
76 | 563k | int64_t pos; |
77 | 563k | uint16_t sync; |
78 | 563k | int ret, size; |
79 | | |
80 | 563k | if (avio_feof(s->pb)) |
81 | 1.56k | return AVERROR_EOF; |
82 | | |
83 | 561k | pos = avio_tell(s->pb); |
84 | 561k | sync = avio_rb16(pb); |
85 | 561k | size = avio_rb16(pb); |
86 | 561k | if (size == 0xffff) |
87 | 1.34k | size = avio_rb24(pb); |
88 | | |
89 | 561k | ret = av_get_packet(pb, pkt, size); |
90 | 561k | pkt->pos = pos; |
91 | 561k | pkt->stream_index = 0; |
92 | | |
93 | 561k | if (sync == 0xAC41) |
94 | 4.29k | avio_skip(pb, 2); |
95 | | |
96 | 561k | return ret; |
97 | 563k | } |
98 | | |
99 | | const FFInputFormat ff_ac4_demuxer = { |
100 | | .p.name = "ac4", |
101 | | .p.long_name = NULL_IF_CONFIG_SMALL("raw AC-4"), |
102 | | .p.flags = AVFMT_GENERIC_INDEX, |
103 | | .p.extensions = "ac4", |
104 | | .read_probe = ac4_probe, |
105 | | .read_header = ac4_read_header, |
106 | | .read_packet = ac4_read_packet, |
107 | | }; |