/src/ffmpeg/libavformat/eacdata.c
Line | Count | Source |
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 "demux.h" |
34 | | #include "internal.h" |
35 | | |
36 | | #include "libavutil/channel_layout.h" |
37 | | |
38 | | typedef struct CdataDemuxContext { |
39 | | unsigned int channels; |
40 | | unsigned int audio_pts; |
41 | | } CdataDemuxContext; |
42 | | |
43 | | static int cdata_probe(const AVProbeData *p) |
44 | 964k | { |
45 | 964k | const uint8_t *b = p->buf; |
46 | | |
47 | 964k | if (b[0] == 0x04 && (b[1] == 0x00 || b[1] == 0x04 || b[1] == 0x0C || b[1] == 0x14)) |
48 | 1.87k | return AVPROBE_SCORE_MAX/8; |
49 | 963k | return 0; |
50 | 964k | } |
51 | | |
52 | | static int cdata_read_header(AVFormatContext *s) |
53 | 786 | { |
54 | 786 | CdataDemuxContext *cdata = s->priv_data; |
55 | 786 | AVIOContext *pb = s->pb; |
56 | 786 | unsigned int sample_rate, header; |
57 | 786 | AVStream *st; |
58 | 786 | AVChannelLayout channel_layout; |
59 | | |
60 | 786 | header = avio_rb16(pb); |
61 | 786 | switch (header) { |
62 | 554 | case 0x0400: |
63 | 554 | channel_layout = (AVChannelLayout){ .nb_channels = 1, .order = AV_CHANNEL_ORDER_UNSPEC }; |
64 | 554 | break; |
65 | 25 | case 0x0404: |
66 | 25 | channel_layout = (AVChannelLayout){ .nb_channels = 2, .order = AV_CHANNEL_ORDER_UNSPEC }; |
67 | 25 | break; |
68 | 16 | case 0x040C: |
69 | 16 | channel_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD; break; |
70 | 42 | case 0x0414: |
71 | 42 | channel_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK; break; |
72 | 149 | default: |
73 | 149 | av_log(s, AV_LOG_INFO, "unknown header 0x%04x\n", header); |
74 | 149 | return -1; |
75 | 786 | }; |
76 | 637 | cdata->channels = channel_layout.nb_channels; |
77 | | |
78 | 637 | sample_rate = avio_rb16(pb); |
79 | 637 | avio_skip(pb, (avio_r8(pb) & 0x20) ? 15 : 11); |
80 | | |
81 | 637 | st = avformat_new_stream(s, NULL); |
82 | 637 | if (!st) |
83 | 0 | return AVERROR(ENOMEM); |
84 | 637 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
85 | 637 | st->codecpar->codec_tag = 0; /* no fourcc */ |
86 | 637 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_EA_XAS; |
87 | 637 | st->codecpar->ch_layout = channel_layout; |
88 | 637 | st->codecpar->sample_rate = sample_rate; |
89 | 637 | avpriv_set_pts_info(st, 64, 1, sample_rate); |
90 | | |
91 | 637 | cdata->audio_pts = 0; |
92 | 637 | return 0; |
93 | 637 | } |
94 | | |
95 | | static int cdata_read_packet(AVFormatContext *s, AVPacket *pkt) |
96 | 408k | { |
97 | 408k | CdataDemuxContext *cdata = s->priv_data; |
98 | 408k | int packet_size = 76*cdata->channels; |
99 | | |
100 | 408k | int ret = av_get_packet(s->pb, pkt, packet_size); |
101 | 408k | if (ret < 0) |
102 | 971 | return ret; |
103 | 407k | pkt->pts = cdata->audio_pts++; |
104 | 407k | return 0; |
105 | 408k | } |
106 | | |
107 | | const FFInputFormat ff_ea_cdata_demuxer = { |
108 | | .p.name = "ea_cdata", |
109 | | .p.long_name = NULL_IF_CONFIG_SMALL("Electronic Arts cdata"), |
110 | | .p.extensions = "cdata", |
111 | | .priv_data_size = sizeof(CdataDemuxContext), |
112 | | .read_probe = cdata_probe, |
113 | | .read_header = cdata_read_header, |
114 | | .read_packet = cdata_read_packet, |
115 | | }; |