/src/ffmpeg/libavformat/xvag.c
Line | Count | Source |
1 | | /* |
2 | | * XVAG demuxer |
3 | | * Copyright (c) 2015 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/bswap.h" |
23 | | #include "libavcodec/internal.h" |
24 | | #include "avformat.h" |
25 | | #include "demux.h" |
26 | | #include "internal.h" |
27 | | |
28 | | static int xvag_probe(const AVProbeData *p) |
29 | 971k | { |
30 | 971k | if (memcmp(p->buf, "XVAG", 4) || |
31 | 1.33k | memcmp(p->buf+32, "fmat", 4)) |
32 | 971k | return 0; |
33 | | |
34 | 251 | return AVPROBE_SCORE_MAX; |
35 | 971k | } |
36 | | |
37 | | static int xvag_read_header(AVFormatContext *s) |
38 | 2.25k | { |
39 | 2.25k | unsigned offset, big_endian, codec; |
40 | 2.25k | AVStream *st; |
41 | | |
42 | 2.25k | avio_skip(s->pb, 4); |
43 | | |
44 | 2.25k | st = avformat_new_stream(s, NULL); |
45 | 2.25k | if (!st) |
46 | 0 | return AVERROR(ENOMEM); |
47 | | |
48 | 2.25k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
49 | | |
50 | 2.25k | offset = avio_rl32(s->pb); |
51 | 2.25k | big_endian = offset > av_bswap32(offset); |
52 | 2.25k | if (big_endian) { |
53 | 1.25k | offset = av_bswap32(offset); |
54 | 1.25k | avio_skip(s->pb, 28); |
55 | 1.25k | codec = avio_rb32(s->pb); |
56 | 1.25k | st->codecpar->ch_layout.nb_channels = avio_rb32(s->pb); |
57 | 1.25k | avio_skip(s->pb, 4); |
58 | 1.25k | st->duration = avio_rb32(s->pb); |
59 | 1.25k | avio_skip(s->pb, 8); |
60 | 1.25k | st->codecpar->sample_rate = avio_rb32(s->pb); |
61 | 1.25k | } else { |
62 | 1.00k | avio_skip(s->pb, 28); |
63 | 1.00k | codec = avio_rl32(s->pb); |
64 | 1.00k | st->codecpar->ch_layout.nb_channels = avio_rl32(s->pb); |
65 | 1.00k | avio_skip(s->pb, 4); |
66 | 1.00k | st->duration = avio_rl32(s->pb); |
67 | 1.00k | avio_skip(s->pb, 8); |
68 | 1.00k | st->codecpar->sample_rate = avio_rl32(s->pb); |
69 | 1.00k | } |
70 | | |
71 | 2.25k | if (st->codecpar->sample_rate <= 0) |
72 | 235 | return AVERROR_INVALIDDATA; |
73 | 2.01k | if (st->codecpar->ch_layout.nb_channels <= 0 || |
74 | 1.97k | st->codecpar->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) |
75 | 71 | return AVERROR_INVALIDDATA; |
76 | | |
77 | 1.94k | switch (codec) { |
78 | 1.91k | case 0x1c: |
79 | 1.91k | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX; |
80 | 1.91k | st->codecpar->block_align = 16 * st->codecpar->ch_layout.nb_channels; |
81 | 1.91k | break; |
82 | 29 | default: |
83 | 29 | avpriv_request_sample(s, "codec %X", codec); |
84 | 29 | return AVERROR_PATCHWELCOME; |
85 | 1.94k | }; |
86 | | |
87 | 1.91k | avio_skip(s->pb, offset - avio_tell(s->pb)); |
88 | | |
89 | 1.91k | if (avio_rb16(s->pb) == 0xFFFB) { |
90 | 1.26k | st->codecpar->codec_id = AV_CODEC_ID_MP3; |
91 | 1.26k | st->codecpar->block_align = 0x1000; |
92 | 1.26k | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW; |
93 | 1.26k | } |
94 | | |
95 | 1.91k | avio_skip(s->pb, -2); |
96 | 1.91k | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); |
97 | | |
98 | 1.91k | return 0; |
99 | 1.94k | } |
100 | | |
101 | | static int xvag_read_packet(AVFormatContext *s, AVPacket *pkt) |
102 | 67.7k | { |
103 | 67.7k | AVCodecParameters *par = s->streams[0]->codecpar; |
104 | | |
105 | 67.7k | return av_get_packet(s->pb, pkt, par->block_align); |
106 | 67.7k | } |
107 | | |
108 | | const FFInputFormat ff_xvag_demuxer = { |
109 | | .p.name = "xvag", |
110 | | .p.long_name = NULL_IF_CONFIG_SMALL("Sony PS3 XVAG"), |
111 | | .p.extensions = "xvag", |
112 | | .read_probe = xvag_probe, |
113 | | .read_header = xvag_read_header, |
114 | | .read_packet = xvag_read_packet, |
115 | | }; |