Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2011 Justin Ruggles | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file | ||
23 | * CRI ADX demuxer | ||
24 | */ | ||
25 | |||
26 | #include "libavutil/intreadwrite.h" | ||
27 | #include "avformat.h" | ||
28 | #include "demux.h" | ||
29 | #include "internal.h" | ||
30 | #include "rawdec.h" | ||
31 | |||
32 | #define BLOCK_SIZE 18 | ||
33 | #define BLOCK_SAMPLES 32 | ||
34 | |||
35 | typedef struct ADXDemuxerContext { | ||
36 | FFRawDemuxerContext rawctx; | ||
37 | int header_size; | ||
38 | } ADXDemuxerContext; | ||
39 | |||
40 | 7279 | static int adx_probe(const AVProbeData *p) | |
41 | { | ||
42 | int offset; | ||
43 |
2/2✓ Branch 0 taken 7278 times.
✓ Branch 1 taken 1 times.
|
7279 | if (AV_RB16(p->buf) != 0x8000) |
44 | 7278 | return 0; | |
45 | 1 | offset = AV_RB16(&p->buf[2]); | |
46 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if ( offset < 8 |
47 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | || offset > p->buf_size - 4 |
48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | || memcmp(p->buf + offset - 2, "(c)CRI", 6)) |
49 | ✗ | return 0; | |
50 | 1 | return AVPROBE_SCORE_MAX * 3 / 4; | |
51 | } | ||
52 | |||
53 | 66 | static int adx_read_packet(AVFormatContext *s, AVPacket *pkt) | |
54 | { | ||
55 | 66 | ADXDemuxerContext *c = s->priv_data; | |
56 | 66 | AVCodecParameters *par = s->streams[0]->codecpar; | |
57 | int ret, size; | ||
58 | |||
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (par->codec_id == AV_CODEC_ID_AHX) |
60 | ✗ | return ff_raw_read_partial_packet(s, pkt); | |
61 | |||
62 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 65 times.
|
66 | if (avio_feof(s->pb)) |
63 | 1 | return AVERROR_EOF; | |
64 | |||
65 | 65 | size = BLOCK_SIZE * par->ch_layout.nb_channels; | |
66 | |||
67 | 65 | pkt->pos = avio_tell(s->pb); | |
68 | 65 | pkt->stream_index = 0; | |
69 | |||
70 | 65 | ret = av_get_packet(s->pb, pkt, size * 128); | |
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (ret < 0) |
72 | ✗ | return ret; | |
73 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
65 | if ((ret % size) && ret >= size) { |
74 | 1 | size = ret - (ret % size); | |
75 | 1 | av_shrink_packet(pkt, size); | |
76 | 1 | pkt->flags &= ~AV_PKT_FLAG_CORRUPT; | |
77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | } else if (ret < size) { |
78 | ✗ | return AVERROR(EIO); | |
79 | } else { | ||
80 | 64 | size = ret; | |
81 | } | ||
82 | |||
83 | 65 | pkt->duration = size / (BLOCK_SIZE * par->ch_layout.nb_channels); | |
84 | 65 | pkt->pts = (pkt->pos - c->header_size) / (BLOCK_SIZE * par->ch_layout.nb_channels); | |
85 | |||
86 | 65 | return 0; | |
87 | } | ||
88 | |||
89 | 1 | static int adx_read_header(AVFormatContext *s) | |
90 | { | ||
91 | 1 | ADXDemuxerContext *c = s->priv_data; | |
92 | AVCodecParameters *par; | ||
93 | FFStream *sti; | ||
94 | int ret; | ||
95 | int channels; | ||
96 | |||
97 | 1 | AVStream *st = avformat_new_stream(s, NULL); | |
98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!st) |
99 | ✗ | return AVERROR(ENOMEM); | |
100 | 1 | par = s->streams[0]->codecpar; | |
101 | |||
102 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (avio_rb16(s->pb) != 0x8000) |
103 | ✗ | return AVERROR_INVALIDDATA; | |
104 | 1 | c->header_size = avio_rb16(s->pb) + 4; | |
105 | 1 | avio_seek(s->pb, -4, SEEK_CUR); | |
106 | |||
107 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0) |
108 | ✗ | return ret; | |
109 | |||
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (par->extradata_size < 12) { |
111 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n"); | |
112 | ✗ | return AVERROR_INVALIDDATA; | |
113 | } | ||
114 | 1 | channels = AV_RB8 (par->extradata + 7); | |
115 | 1 | par->sample_rate = AV_RB32(par->extradata + 8); | |
116 | |||
117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (channels <= 0) { |
118 | ✗ | av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", channels); | |
119 | ✗ | return AVERROR_INVALIDDATA; | |
120 | } | ||
121 | |||
122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (par->sample_rate <= 0) { |
123 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate); | |
124 | ✗ | return AVERROR_INVALIDDATA; | |
125 | } | ||
126 | |||
127 | 1 | sti = ffstream(st); | |
128 | 1 | par->ch_layout.nb_channels = channels; | |
129 | 1 | par->codec_type = AVMEDIA_TYPE_AUDIO; | |
130 |
1/3✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1 | switch (par->extradata[4]) { |
131 | 1 | case 3: | |
132 | 1 | sti->need_parsing = AVSTREAM_PARSE_FULL_RAW; | |
133 | 1 | par->codec_id = AV_CODEC_ID_ADPCM_ADX; | |
134 | 1 | par->bit_rate = (int64_t)par->sample_rate * par->ch_layout.nb_channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES; | |
135 | 1 | avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate); | |
136 | 1 | break; | |
137 | ✗ | case 16: | |
138 | ✗ | sti->need_parsing = AVSTREAM_PARSE_FULL_RAW; | |
139 | ✗ | par->codec_id = AV_CODEC_ID_AHX; | |
140 | ✗ | avpriv_set_pts_info(st, 64, 1, par->sample_rate); | |
141 | ✗ | break; | |
142 | ✗ | default: | |
143 | ✗ | av_log(s, AV_LOG_ERROR, "Unsupported format: %d\n", par->extradata[4]); | |
144 | ✗ | return AVERROR_INVALIDDATA; | |
145 | } | ||
146 | |||
147 | 1 | return 0; | |
148 | } | ||
149 | |||
150 | const FFInputFormat ff_adx_demuxer = { | ||
151 | .p.name = "adx", | ||
152 | .p.long_name = NULL_IF_CONFIG_SMALL("CRI ADX"), | ||
153 | .p.extensions = "adx", | ||
154 | .p.priv_class = &ff_raw_demuxer_class, | ||
155 | .p.flags = AVFMT_GENERIC_INDEX, | ||
156 | .read_probe = adx_probe, | ||
157 | .priv_data_size = sizeof(ADXDemuxerContext), | ||
158 | .read_header = adx_read_header, | ||
159 | .read_packet = adx_read_packet, | ||
160 | }; | ||
161 |