3 * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/bswap.h"
28 #include "libavutil/intreadwrite.h"
32 #define MTV_ASUBCHUNK_DATA_SIZE 500
33 #define MTV_HEADER_SIZE 512
34 #define MTV_AUDIO_PADDING_SIZE 12
35 #define AUDIO_SAMPLING_RATE 44100
37 typedef struct MTVDemuxContext {
39 unsigned int file_size; ///< filesize, not always right
40 unsigned int segments; ///< number of 512 byte segments
41 unsigned int audio_identifier; ///< 'MP3' on all files I have seen
42 unsigned int audio_br; ///< bitrate of audio channel (mp3)
43 unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
44 unsigned int img_bpp; ///< frame bits per pixel
45 unsigned int img_width;
46 unsigned int img_height;
47 unsigned int img_segment_size; ///< size of image segment
48 unsigned int video_fps;
49 unsigned int full_segment_size;
53 static int mtv_probe(AVProbeData *p)
55 /* we need at least 57 bytes from the header
56 * to try parsing all required fields
62 if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V')
65 /* Check for nonzero in bpp and (width|height) header fields */
66 if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
69 /* If width or height are 0 then imagesize header field should not */
70 if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
72 if(!!AV_RL16(&p->buf[56]))
73 return AVPROBE_SCORE_EXTENSION;
79 return AVPROBE_SCORE_EXTENSION / 2; // But we are going to assume 16bpp anyway ..
81 /* We had enough data to parse header values
82 * but we expect to be able to get 512 bytes
83 * of header to be sure.
85 if (p->buf_size < MTV_HEADER_SIZE)
86 return AVPROBE_SCORE_EXTENSION;
88 return AVPROBE_SCORE_MAX;
91 static int mtv_read_header(AVFormatContext *s)
93 MTVDemuxContext *mtv = s->priv_data;
94 AVIOContext *pb = s->pb;
96 unsigned int audio_subsegments;
99 mtv->file_size = avio_rl32(pb);
100 mtv->segments = avio_rl32(pb);
102 mtv->audio_identifier = avio_rl24(pb);
103 mtv->audio_br = avio_rl16(pb);
104 mtv->img_colorfmt = avio_rl24(pb);
105 mtv->img_bpp = avio_r8(pb);
106 mtv->img_width = avio_rl16(pb);
107 mtv->img_height = avio_rl16(pb);
108 mtv->img_segment_size = avio_rl16(pb);
110 /* Calculate width and height if missing from header */
113 if(!mtv->img_width && mtv->img_height)
114 mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
117 if(!mtv->img_height && mtv->img_width)
118 mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
121 if(!mtv->img_height || !mtv->img_width || !mtv->img_segment_size){
122 av_log(s, AV_LOG_ERROR, "width or height or segment_size is invalid and I cannot calculate them from other information\n");
123 return AVERROR(EINVAL);
127 audio_subsegments = avio_rl16(pb);
129 if (audio_subsegments == 0) {
130 avpriv_request_sample(s, "MTV files without audio");
131 return AVERROR_PATCHWELCOME;
134 mtv->full_segment_size =
135 audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
136 mtv->img_segment_size;
137 mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
139 // FIXME Add sanity check here
141 // all systems go! init decoders
143 // video - raw rgb565
145 st = avformat_new_stream(s, NULL);
147 return AVERROR(ENOMEM);
149 avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
150 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
151 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
152 st->codec->pix_fmt = AV_PIX_FMT_RGB565BE;
153 st->codec->width = mtv->img_width;
154 st->codec->height = mtv->img_height;
155 st->codec->sample_rate = mtv->video_fps;
156 st->codec->extradata = av_strdup("BottomUp");
157 st->codec->extradata_size = 9;
161 st = avformat_new_stream(s, NULL);
163 return AVERROR(ENOMEM);
165 avpriv_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
166 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
167 st->codec->codec_id = AV_CODEC_ID_MP3;
168 st->codec->bit_rate = mtv->audio_br;
169 st->need_parsing = AVSTREAM_PARSE_FULL;
173 if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
180 static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
182 MTVDemuxContext *mtv = s->priv_data;
183 AVIOContext *pb = s->pb;
186 if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
188 avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
190 ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
194 pkt->pos -= MTV_AUDIO_PADDING_SIZE;
195 pkt->stream_index = 1;
199 ret = av_get_packet(pb, pkt, mtv->img_segment_size);
203 pkt->stream_index = 0;
209 AVInputFormat ff_mtv_demuxer = {
211 .long_name = NULL_IF_CONFIG_SMALL("MTV"),
212 .priv_data_size = sizeof(MTVDemuxContext),
213 .read_probe = mtv_probe,
214 .read_header = mtv_read_header,
215 .read_packet = mtv_read_packet,