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)
56 if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V')
59 /* Check for nonzero in bpp and (width|height) header fields */
60 if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
63 /* If width or height are 0 then imagesize header field should not */
64 if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
66 if(!!AV_RL16(&p->buf[56]))
67 return AVPROBE_SCORE_MAX/2;
73 return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway ..
75 return AVPROBE_SCORE_MAX;
78 static int mtv_read_header(AVFormatContext *s)
80 MTVDemuxContext *mtv = s->priv_data;
81 AVIOContext *pb = s->pb;
83 unsigned int audio_subsegments;
86 mtv->file_size = avio_rl32(pb);
87 mtv->segments = avio_rl32(pb);
89 mtv->audio_identifier = avio_rl24(pb);
90 mtv->audio_br = avio_rl16(pb);
91 mtv->img_colorfmt = avio_rl24(pb);
92 mtv->img_bpp = avio_r8(pb);
93 mtv->img_width = avio_rl16(pb);
94 mtv->img_height = avio_rl16(pb);
95 mtv->img_segment_size = avio_rl16(pb);
97 /* Calculate width and height if missing from header */
100 if(!mtv->img_width && mtv->img_height)
101 mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
104 if(!mtv->img_height && mtv->img_width)
105 mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
108 if(!mtv->img_height || !mtv->img_width){
109 av_log(s, AV_LOG_ERROR, "width or height is invalid and I cannot calculate them from other information\n");
110 return AVERROR(EINVAL);
114 audio_subsegments = avio_rl16(pb);
116 if (audio_subsegments == 0) {
117 av_log_ask_for_sample(s, "MTV files without audio are not supported\n");
118 return AVERROR_INVALIDDATA;
121 mtv->full_segment_size =
122 audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
123 mtv->img_segment_size;
124 mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
126 // FIXME Add sanity check here
128 // all systems go! init decoders
130 // video - raw rgb565
132 st = avformat_new_stream(s, NULL);
134 return AVERROR(ENOMEM);
136 avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
137 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
138 st->codec->codec_id = CODEC_ID_RAWVIDEO;
139 st->codec->pix_fmt = PIX_FMT_RGB565;
140 st->codec->width = mtv->img_width;
141 st->codec->height = mtv->img_height;
142 st->codec->sample_rate = mtv->video_fps;
143 st->codec->extradata = av_strdup("BottomUp");
144 st->codec->extradata_size = 9;
148 st = avformat_new_stream(s, NULL);
150 return AVERROR(ENOMEM);
152 avpriv_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
153 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
154 st->codec->codec_id = CODEC_ID_MP3;
155 st->codec->bit_rate = mtv->audio_br;
156 st->need_parsing = AVSTREAM_PARSE_FULL;
160 if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
167 static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
169 MTVDemuxContext *mtv = s->priv_data;
170 AVIOContext *pb = s->pb;
176 if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
178 avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
180 ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
184 pkt->pos -= MTV_AUDIO_PADDING_SIZE;
185 pkt->stream_index = 1;
189 ret = av_get_packet(pb, pkt, mtv->img_segment_size);
195 /* pkt->data is GGGRRRR BBBBBGGG
196 * and we need RRRRRGGG GGGBBBBB
197 * for PIX_FMT_RGB565 so here we
198 * just swap bytes as they come
202 *((uint16_t *)pkt->data+i) = av_bswap16(*((uint16_t *)pkt->data+i));
204 pkt->stream_index = 0;
210 AVInputFormat ff_mtv_demuxer = {
212 .long_name = NULL_IF_CONFIG_SMALL("MTV format"),
213 .priv_data_size = sizeof(MTVDemuxContext),
214 .read_probe = mtv_probe,
215 .read_header = mtv_read_header,
216 .read_packet = mtv_read_packet,