xref: /third_party/ffmpeg/libavformat/eacdata.c (revision cabdff1a)
1/*
2 * Electronic Arts .cdata file Demuxer
3 * Copyright (c) 2007 Peter Ross
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/**
23 * @file
24 * Electronic Arts cdata Format Demuxer
25 * by Peter Ross (pross@xvid.org)
26 *
27 * Technical details here:
28 *  http://wiki.multimedia.cx/index.php?title=EA_Command_And_Conquer_3_Audio_Codec
29 */
30
31#include "libavutil/channel_layout.h"
32#include "avformat.h"
33#include "internal.h"
34
35#include "libavutil/channel_layout.h"
36
37typedef struct CdataDemuxContext {
38  unsigned int channels;
39  unsigned int audio_pts;
40} CdataDemuxContext;
41
42static int cdata_probe(const AVProbeData *p)
43{
44    const uint8_t *b = p->buf;
45
46    if (b[0] == 0x04 && (b[1] == 0x00 || b[1] == 0x04 || b[1] == 0x0C || b[1] == 0x14))
47        return AVPROBE_SCORE_MAX/8;
48    return 0;
49}
50
51static int cdata_read_header(AVFormatContext *s)
52{
53    CdataDemuxContext *cdata = s->priv_data;
54    AVIOContext *pb = s->pb;
55    unsigned int sample_rate, header;
56    AVStream *st;
57    AVChannelLayout channel_layout;
58
59    header = avio_rb16(pb);
60    switch (header) {
61        case 0x0400:
62            channel_layout = (AVChannelLayout){ .nb_channels = 1, .order = AV_CHANNEL_ORDER_UNSPEC };
63            break;
64        case 0x0404:
65            channel_layout  = (AVChannelLayout){ .nb_channels = 2, .order = AV_CHANNEL_ORDER_UNSPEC };
66            break;
67        case 0x040C:
68            channel_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD;         break;
69        case 0x0414:
70            channel_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK; break;
71        default:
72            av_log(s, AV_LOG_INFO, "unknown header 0x%04x\n", header);
73            return -1;
74    };
75    cdata->channels = channel_layout.nb_channels;
76
77    sample_rate = avio_rb16(pb);
78    avio_skip(pb, (avio_r8(pb) & 0x20) ? 15 : 11);
79
80    st = avformat_new_stream(s, NULL);
81    if (!st)
82        return AVERROR(ENOMEM);
83    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
84    st->codecpar->codec_tag = 0; /* no fourcc */
85    st->codecpar->codec_id = AV_CODEC_ID_ADPCM_EA_XAS;
86    st->codecpar->ch_layout = channel_layout;
87    st->codecpar->sample_rate = sample_rate;
88    avpriv_set_pts_info(st, 64, 1, sample_rate);
89
90    cdata->audio_pts = 0;
91    return 0;
92}
93
94static int cdata_read_packet(AVFormatContext *s, AVPacket *pkt)
95{
96    CdataDemuxContext *cdata = s->priv_data;
97    int packet_size = 76*cdata->channels;
98
99    int ret = av_get_packet(s->pb, pkt, packet_size);
100    if (ret < 0)
101        return ret;
102    pkt->pts = cdata->audio_pts++;
103    return 0;
104}
105
106const AVInputFormat ff_ea_cdata_demuxer = {
107    .name           = "ea_cdata",
108    .long_name      = NULL_IF_CONFIG_SMALL("Electronic Arts cdata"),
109    .priv_data_size = sizeof(CdataDemuxContext),
110    .read_probe     = cdata_probe,
111    .read_header    = cdata_read_header,
112    .read_packet    = cdata_read_packet,
113    .extensions = "cdata",
114};
115