]> git.sesse.net Git - ffmpeg/blob - libavformat/rsd.c
avformat/dv: fix timestamps of audio packets in case of dropped corrupt audio frames
[ffmpeg] / libavformat / rsd.c
1 /*
2  * RSD demuxer
3  * Copyright (c) 2013 James Almer
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 "libavcodec/bytestream.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "avio.h"
26 #include "internal.h"
27
28 static const AVCodecTag rsd_tags[] = {
29     { AV_CODEC_ID_ADPCM_PSX,       MKTAG('V','A','G',' ') },
30     { AV_CODEC_ID_ADPCM_THP_LE,    MKTAG('G','A','D','P') },
31     { AV_CODEC_ID_ADPCM_THP,       MKTAG('W','A','D','P') },
32     { AV_CODEC_ID_ADPCM_IMA_RAD,   MKTAG('R','A','D','P') },
33     { AV_CODEC_ID_ADPCM_IMA_WAV,   MKTAG('X','A','D','P') },
34     { AV_CODEC_ID_PCM_S16BE,       MKTAG('P','C','M','B') },
35     { AV_CODEC_ID_PCM_S16LE,       MKTAG('P','C','M',' ') },
36     { AV_CODEC_ID_XMA2,            MKTAG('X','M','A',' ') },
37     { AV_CODEC_ID_NONE, 0 },
38 };
39
40 static const uint32_t rsd_unsupported_tags[] = {
41     MKTAG('O','G','G',' '),
42 };
43
44 static int rsd_probe(const AVProbeData *p)
45 {
46     if (memcmp(p->buf, "RSD", 3) || p->buf[3] - '0' < 2 || p->buf[3] - '0' > 6)
47         return 0;
48     if (AV_RL32(p->buf +  8) > 256 || !AV_RL32(p->buf +  8))
49         return AVPROBE_SCORE_MAX / 8;
50     if (AV_RL32(p->buf + 16) > 8*48000 || !AV_RL32(p->buf + 16))
51         return AVPROBE_SCORE_MAX / 8;
52     return AVPROBE_SCORE_MAX;
53 }
54
55 static int rsd_read_header(AVFormatContext *s)
56 {
57     AVIOContext *pb = s->pb;
58     int i, ret, version, start = 0x800;
59     AVCodecParameters *par;
60     AVStream *st = avformat_new_stream(s, NULL);
61
62     if (!st)
63         return AVERROR(ENOMEM);
64
65     avio_skip(pb, 3); // "RSD"
66     version = avio_r8(pb) - '0';
67
68     par = st->codecpar;
69     par->codec_type = AVMEDIA_TYPE_AUDIO;
70     par->codec_tag  = avio_rl32(pb);
71     par->codec_id   = ff_codec_get_id(rsd_tags, par->codec_tag);
72     if (!par->codec_id) {
73         const char *tag_buf = av_fourcc2str(par->codec_tag);
74         for (i=0; i < FF_ARRAY_ELEMS(rsd_unsupported_tags); i++) {
75             if (par->codec_tag == rsd_unsupported_tags[i]) {
76                 avpriv_request_sample(s, "Codec tag: %s", tag_buf);
77                 return AVERROR_PATCHWELCOME;
78             }
79         }
80         av_log(s, AV_LOG_ERROR, "Unknown codec tag: %s\n", tag_buf);
81         return AVERROR_INVALIDDATA;
82     }
83
84     par->channels = avio_rl32(pb);
85     if (par->channels <= 0 || par->channels > INT_MAX / 36) {
86         av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", par->channels);
87         return AVERROR_INVALIDDATA;
88     }
89
90     avio_skip(pb, 4); // Bit depth
91     par->sample_rate = avio_rl32(pb);
92     if (!par->sample_rate)
93         return AVERROR_INVALIDDATA;
94
95     avio_skip(pb, 4); // Unknown
96
97     switch (par->codec_id) {
98     case AV_CODEC_ID_XMA2:
99         par->block_align = 2048;
100         if ((ret = ff_alloc_extradata(par, 34)) < 0)
101             return ret;
102         memset(par->extradata, 0, 34);
103         break;
104     case AV_CODEC_ID_ADPCM_PSX:
105         par->block_align = 16 * par->channels;
106         break;
107     case AV_CODEC_ID_ADPCM_IMA_RAD:
108         par->block_align = 20 * par->channels;
109         break;
110     case AV_CODEC_ID_ADPCM_IMA_WAV:
111         if (version == 2)
112             start = avio_rl32(pb);
113
114         par->bits_per_coded_sample = 4;
115         par->block_align = 36 * par->channels;
116         break;
117     case AV_CODEC_ID_ADPCM_THP_LE:
118         /* RSD3GADP is mono, so only alloc enough memory
119            to store the coeff table for a single channel. */
120
121         start = avio_rl32(pb);
122
123         if ((ret = ff_get_extradata(s, par, s->pb, 32)) < 0)
124             return ret;
125         break;
126     case AV_CODEC_ID_ADPCM_THP:
127         par->block_align = 8 * par->channels;
128         avio_skip(s->pb, 0x1A4 - avio_tell(s->pb));
129
130         if ((ret = ff_alloc_extradata(st->codecpar, 32 * par->channels)) < 0)
131             return ret;
132
133         for (i = 0; i < par->channels; i++) {
134             avio_read(s->pb, st->codecpar->extradata + 32 * i, 32);
135             avio_skip(s->pb, 8);
136         }
137         break;
138     case AV_CODEC_ID_PCM_S16LE:
139     case AV_CODEC_ID_PCM_S16BE:
140         if (version != 4)
141             start = avio_rl32(pb);
142
143         break;
144     }
145     if (start < 0)
146         return AVERROR_INVALIDDATA;
147
148     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
149         int64_t remaining = avio_size(pb);
150
151         if (remaining >= start && remaining - start <= INT_MAX)
152             switch (par->codec_id) {
153             case AV_CODEC_ID_ADPCM_PSX:
154             case AV_CODEC_ID_ADPCM_IMA_RAD:
155             case AV_CODEC_ID_ADPCM_IMA_WAV:
156             case AV_CODEC_ID_ADPCM_THP_LE:
157                 st->duration = av_get_audio_frame_duration2(par, remaining - start);
158                 break;
159             case AV_CODEC_ID_ADPCM_THP:
160                 st->duration = (remaining - start) / (8 * par->channels) * 14;
161                 break;
162             case AV_CODEC_ID_PCM_S16LE:
163             case AV_CODEC_ID_PCM_S16BE:
164                 st->duration = (remaining - start) / 2 / par->channels;
165             }
166     }
167
168     avio_skip(pb, start - avio_tell(pb));
169     if (par->codec_id == AV_CODEC_ID_XMA2) {
170         avio_skip(pb, avio_rb32(pb) + avio_rb32(pb));
171         st->duration = avio_rb32(pb);
172     }
173
174     avpriv_set_pts_info(st, 64, 1, par->sample_rate);
175
176     return 0;
177 }
178
179 static int rsd_read_packet(AVFormatContext *s, AVPacket *pkt)
180 {
181     AVCodecParameters *par = s->streams[0]->codecpar;
182     int ret, size = 1024;
183     int64_t pos;
184
185     if (avio_feof(s->pb))
186         return AVERROR_EOF;
187
188     pos = avio_tell(s->pb);
189     if (par->codec_id == AV_CODEC_ID_ADPCM_IMA_RAD ||
190         par->codec_id == AV_CODEC_ID_ADPCM_PSX     ||
191         par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
192         par->codec_id == AV_CODEC_ID_XMA2) {
193         ret = av_get_packet(s->pb, pkt, par->block_align);
194     } else if (par->codec_tag == MKTAG('W','A','D','P') &&
195                par->channels > 1) {
196         int i, ch;
197
198         ret = av_new_packet(pkt, par->block_align);
199         if (ret < 0)
200             return ret;
201         for (i = 0; i < 4; i++) {
202             for (ch = 0; ch < par->channels; ch++) {
203                 pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
204                 pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
205             }
206         }
207         ret = 0;
208     } else {
209         ret = av_get_packet(s->pb, pkt, size);
210     }
211
212     if (par->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
213         pkt->duration = (pkt->data[0] >> 2) * 512;
214
215     pkt->pos = pos;
216     pkt->stream_index = 0;
217
218     return ret;
219 }
220
221 AVInputFormat ff_rsd_demuxer = {
222     .name           =   "rsd",
223     .long_name      =   NULL_IF_CONFIG_SMALL("GameCube RSD"),
224     .read_probe     =   rsd_probe,
225     .read_header    =   rsd_read_header,
226     .read_packet    =   rsd_read_packet,
227     .extensions     =   "rsd",
228     .codec_tag      =   (const AVCodecTag* const []){rsd_tags, 0},
229     .flags          =   AVFMT_GENERIC_INDEX,
230 };