Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * mtv demuxer | ||
3 | * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet | ||
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 | * MTV demuxer. | ||
25 | */ | ||
26 | |||
27 | #include "libavutil/intreadwrite.h" | ||
28 | #include "libavutil/mem.h" | ||
29 | #include "avformat.h" | ||
30 | #include "demux.h" | ||
31 | #include "internal.h" | ||
32 | |||
33 | #define MTV_ASUBCHUNK_DATA_SIZE 500 | ||
34 | #define MTV_HEADER_SIZE 512 | ||
35 | #define MTV_AUDIO_PADDING_SIZE 12 | ||
36 | #define MTV_IMAGE_DEFAULT_BPP 16 | ||
37 | #define MTV_AUDIO_SAMPLING_RATE 44100 | ||
38 | |||
39 | typedef struct MTVDemuxContext { | ||
40 | |||
41 | unsigned int file_size; ///< filesize, not always right | ||
42 | unsigned int segments; ///< number of 512 byte segments | ||
43 | unsigned int audio_identifier; ///< 'MP3' on all files I have seen | ||
44 | unsigned int audio_br; ///< bitrate of audio channel (mp3) | ||
45 | unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555 | ||
46 | unsigned int img_bpp; ///< frame bits per pixel | ||
47 | unsigned int img_width; | ||
48 | unsigned int img_height; | ||
49 | unsigned int img_segment_size; ///< size of image segment | ||
50 | unsigned int video_fps; | ||
51 | unsigned int full_segment_size; | ||
52 | |||
53 | } MTVDemuxContext; | ||
54 | |||
55 | 7186 | static int mtv_probe(const AVProbeData *p) | |
56 | { | ||
57 | /* we need at least 57 bytes from the header | ||
58 | * to try parsing all required fields | ||
59 | */ | ||
60 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7183 times.
|
7186 | if (p->buf_size < 57) |
61 | 3 | return 0; | |
62 | |||
63 | /* Magic is 'AMV' */ | ||
64 |
5/6✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7170 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
7183 | if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V') |
65 | 7182 | return 0; | |
66 | |||
67 | /* Audio magic is always MP3 */ | ||
68 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if (p->buf[43] != 'M' || p->buf[44] != 'P' || p->buf[45] != '3') |
69 | ✗ | return 0; | |
70 | |||
71 | /* Check for nonzero in bpp and (width|height) header fields */ | ||
72 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54]))) |
73 | ✗ | return 0; | |
74 | |||
75 | /* If width or height are 0 then imagesize header field should not */ | ||
76 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54])) |
77 | { | ||
78 | ✗ | if(!!AV_RL16(&p->buf[56])) | |
79 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
80 | else | ||
81 | ✗ | return 0; | |
82 | } | ||
83 | |||
84 | /* Image bpp is not an absolutely required | ||
85 | * field as we latter claim it should be 16 | ||
86 | * no matter what. All samples in the wild | ||
87 | * are RGB565/555. | ||
88 | */ | ||
89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(p->buf[51] != MTV_IMAGE_DEFAULT_BPP) |
90 | ✗ | return AVPROBE_SCORE_EXTENSION / 2; | |
91 | |||
92 | /* We had enough data to parse header values | ||
93 | * but we expect to be able to get 512 bytes | ||
94 | * of header to be sure. | ||
95 | */ | ||
96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (p->buf_size < MTV_HEADER_SIZE) |
97 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
98 | |||
99 | 1 | return AVPROBE_SCORE_MAX; | |
100 | } | ||
101 | |||
102 | 1 | static int mtv_read_header(AVFormatContext *s) | |
103 | { | ||
104 | 1 | MTVDemuxContext *mtv = s->priv_data; | |
105 | 1 | AVIOContext *pb = s->pb; | |
106 | AVStream *st; | ||
107 | unsigned int audio_subsegments; | ||
108 | |||
109 | 1 | avio_skip(pb, 3); | |
110 | 1 | mtv->file_size = avio_rl32(pb); | |
111 | 1 | mtv->segments = avio_rl32(pb); | |
112 | 1 | avio_skip(pb, 32); | |
113 | 1 | mtv->audio_identifier = avio_rl24(pb); | |
114 | 1 | mtv->audio_br = avio_rl16(pb); | |
115 | 1 | mtv->img_colorfmt = avio_rl24(pb); | |
116 | 1 | mtv->img_bpp = avio_r8(pb); | |
117 | 1 | mtv->img_width = avio_rl16(pb); | |
118 | 1 | mtv->img_height = avio_rl16(pb); | |
119 | 1 | mtv->img_segment_size = avio_rl16(pb); | |
120 | |||
121 | /* Assume 16bpp even if claimed otherwise. | ||
122 | * We know its going to be RGBG565/555 anyway | ||
123 | */ | ||
124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (mtv->img_bpp != MTV_IMAGE_DEFAULT_BPP) { |
125 | ✗ | av_log (s, AV_LOG_WARNING, "Header claims %dbpp (!= 16). Ignoring\n", | |
126 | mtv->img_bpp); | ||
127 | ✗ | mtv->img_bpp = MTV_IMAGE_DEFAULT_BPP; | |
128 | } | ||
129 | |||
130 | /* Calculate width and height if missing from header */ | ||
131 | |||
132 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1 | if (!mtv->img_width && mtv->img_height > 0 && mtv->img_bpp >= 8) |
133 | ✗ | mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3) | |
134 | ✗ | / mtv->img_height; | |
135 | |||
136 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1 | if (!mtv->img_height && mtv->img_width > 0 && mtv->img_bpp >= 8) |
137 | ✗ | mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3) | |
138 | ✗ | / mtv->img_width; | |
139 | |||
140 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if(!mtv->img_height || !mtv->img_width || !mtv->img_segment_size){ |
141 | ✗ | av_log(s, AV_LOG_ERROR, "width or height or segment_size is invalid and I cannot calculate them from other information\n"); | |
142 | ✗ | return AVERROR_INVALIDDATA; | |
143 | } | ||
144 | |||
145 | 1 | avio_skip(pb, 4); | |
146 | 1 | audio_subsegments = avio_rl16(pb); | |
147 | |||
148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (audio_subsegments == 0) { |
149 | ✗ | avpriv_request_sample(s, "MTV files without audio"); | |
150 | ✗ | return AVERROR_PATCHWELCOME; | |
151 | } | ||
152 | |||
153 | 1 | mtv->full_segment_size = | |
154 | 1 | audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) + | |
155 | 1 | mtv->img_segment_size; | |
156 | 1 | mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments; | |
157 | |||
158 | // FIXME Add sanity check here | ||
159 | |||
160 | // all systems go! init decoders | ||
161 | |||
162 | // video - raw rgb565 | ||
163 | |||
164 | 1 | st = avformat_new_stream(s, NULL); | |
165 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(!st) |
166 | ✗ | return AVERROR(ENOMEM); | |
167 | |||
168 | 1 | avpriv_set_pts_info(st, 64, 1, mtv->video_fps); | |
169 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
170 | 1 | st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; | |
171 | 1 | st->codecpar->format = AV_PIX_FMT_RGB565BE; | |
172 | 1 | st->codecpar->width = mtv->img_width; | |
173 | 1 | st->codecpar->height = mtv->img_height; | |
174 | 1 | st->codecpar->extradata = av_strdup("BottomUp"); | |
175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!st->codecpar->extradata) |
176 | ✗ | return AVERROR(ENOMEM); | |
177 | 1 | st->codecpar->extradata_size = 9; | |
178 | |||
179 | // audio - mp3 | ||
180 | |||
181 | 1 | st = avformat_new_stream(s, NULL); | |
182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(!st) |
183 | ✗ | return AVERROR(ENOMEM); | |
184 | |||
185 | 1 | avpriv_set_pts_info(st, 64, 1, MTV_AUDIO_SAMPLING_RATE); | |
186 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
187 | 1 | st->codecpar->codec_id = AV_CODEC_ID_MP3; | |
188 | 1 | st->codecpar->bit_rate = mtv->audio_br; | |
189 | 1 | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL; | |
190 | |||
191 | // Jump over header | ||
192 | |||
193 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE) |
194 | ✗ | return AVERROR(EIO); | |
195 | |||
196 | 1 | return 0; | |
197 | |||
198 | } | ||
199 | |||
200 | 123 | static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt) | |
201 | { | ||
202 | 123 | MTVDemuxContext *mtv = s->priv_data; | |
203 | 123 | AVIOContext *pb = s->pb; | |
204 | int ret; | ||
205 | |||
206 |
2/2✓ Branch 2 taken 83 times.
✓ Branch 3 taken 40 times.
|
123 | if((avio_tell(pb) - ffformatcontext(s)->data_offset + mtv->img_segment_size) % mtv->full_segment_size) |
207 | { | ||
208 | 83 | avio_skip(pb, MTV_AUDIO_PADDING_SIZE); | |
209 | |||
210 | 83 | ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE); | |
211 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 80 times.
|
83 | if(ret < 0) |
212 | 3 | return ret; | |
213 | |||
214 | 80 | pkt->pos -= MTV_AUDIO_PADDING_SIZE; | |
215 | 80 | pkt->stream_index = 1; | |
216 | |||
217 | }else | ||
218 | { | ||
219 | 40 | ret = av_get_packet(pb, pkt, mtv->img_segment_size); | |
220 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if(ret < 0) |
221 | ✗ | return ret; | |
222 | |||
223 | 40 | pkt->stream_index = 0; | |
224 | } | ||
225 | |||
226 | 120 | return ret; | |
227 | } | ||
228 | |||
229 | const FFInputFormat ff_mtv_demuxer = { | ||
230 | .p.name = "mtv", | ||
231 | .p.long_name = NULL_IF_CONFIG_SMALL("MTV"), | ||
232 | .priv_data_size = sizeof(MTVDemuxContext), | ||
233 | .read_probe = mtv_probe, | ||
234 | .read_header = mtv_read_header, | ||
235 | .read_packet = mtv_read_packet, | ||
236 | }; | ||
237 |