28146a8c |
/*
* mtv demuxer
* Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** |
ba87f080 |
* @file |
28146a8c |
* MTV demuxer.
*/
|
245976da |
#include "libavutil/bswap.h" |
f122ef36 |
#include "libavutil/intreadwrite.h" |
28146a8c |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
28146a8c |
#define MTV_ASUBCHUNK_DATA_SIZE 500
#define MTV_HEADER_SIZE 512
#define MTV_AUDIO_PADDING_SIZE 12
#define AUDIO_SAMPLING_RATE 44100
typedef struct MTVDemuxContext {
|
7100376f |
unsigned int file_size; ///< filesize, not always right
unsigned int segments; ///< number of 512 byte segments
unsigned int audio_identifier; ///< 'MP3' on all files I have seen |
87e76ae0 |
unsigned int audio_br; ///< bitrate of audio channel (mp3) |
7100376f |
unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
unsigned int img_bpp; ///< frame bits per pixel
unsigned int img_width; //
unsigned int img_height; //
unsigned int img_segment_size; ///< size of image segment
unsigned int video_fps; //
unsigned int full_segment_size; |
28146a8c |
} MTVDemuxContext;
static int mtv_probe(AVProbeData *p)
{
/* Magic is 'AMV' */ |
3dc99a18 |
if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V') |
28146a8c |
return 0;
|
f122ef36 |
/* Check for nonzero in bpp and (width|height) header fields */
if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
return 0;
|
9cf6bbb9 |
/* If width or height are 0 then imagesize header field should not */ |
cbd8a722 |
if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54])) |
9cf6bbb9 |
{
if(!!AV_RL16(&p->buf[56]))
return AVPROBE_SCORE_MAX/2;
else
return 0;
}
|
a8e1cbaf |
if(p->buf[51] != 16) |
87e76ae0 |
return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway .. |
a8e1cbaf |
|
28146a8c |
return AVPROBE_SCORE_MAX;
}
static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
{ |
7100376f |
MTVDemuxContext *mtv = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
7100376f |
AVStream *st;
unsigned int audio_subsegments; |
28146a8c |
|
45a8a02a |
avio_skip(pb, 3); |
b7effd4e |
mtv->file_size = avio_rl32(pb);
mtv->segments = avio_rl32(pb); |
45a8a02a |
avio_skip(pb, 32); |
b7effd4e |
mtv->audio_identifier = avio_rl24(pb);
mtv->audio_br = avio_rl16(pb);
mtv->img_colorfmt = avio_rl24(pb);
mtv->img_bpp = avio_r8(pb);
mtv->img_width = avio_rl16(pb);
mtv->img_height = avio_rl16(pb);
mtv->img_segment_size = avio_rl16(pb); |
9cf6bbb9 |
/* Calculate width and height if missing from header */
|
8b9b6332 |
if(mtv->img_bpp>>3){
if(!mtv->img_width && mtv->img_height) |
9cf6bbb9 |
mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
/ mtv->img_height;
|
8b9b6332 |
if(!mtv->img_height && mtv->img_width) |
9cf6bbb9 |
mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
/ mtv->img_width; |
8b9b6332 |
}
if(!mtv->img_height || !mtv->img_width){
av_log(s, AV_LOG_ERROR, "width or height is invalid and I cannot calculate them from other information\n");
return AVERROR(EINVAL);
} |
9cf6bbb9 |
|
45a8a02a |
avio_skip(pb, 4); |
b7effd4e |
audio_subsegments = avio_rl16(pb); |
feb15cee |
if (audio_subsegments == 0) {
av_log_ask_for_sample(s, "MTV files without audio are not supported\n");
return AVERROR_INVALIDDATA; |
8b9b6332 |
} |
feb15cee |
|
ddca6ab6 |
mtv->full_segment_size =
audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
mtv->img_segment_size;
mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments; |
28146a8c |
|
a45bdbc4 |
// FIXME Add sanity check here |
28146a8c |
|
a45bdbc4 |
// all systems go! init decoders |
28146a8c |
|
a45bdbc4 |
// video - raw rgb565 |
28146a8c |
|
84ad31ff |
st = avformat_new_stream(s, NULL); |
28146a8c |
if(!st) |
769e10f0 |
return AVERROR(ENOMEM); |
28146a8c |
|
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, mtv->video_fps); |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
28146a8c |
st->codec->codec_id = CODEC_ID_RAWVIDEO; |
10fa22cc |
st->codec->pix_fmt = PIX_FMT_RGB565; |
28146a8c |
st->codec->width = mtv->img_width;
st->codec->height = mtv->img_height;
st->codec->sample_rate = mtv->video_fps; |
49056037 |
st->codec->extradata = av_strdup("BottomUp");
st->codec->extradata_size = 9; |
28146a8c |
|
a45bdbc4 |
// audio - mp3 |
28146a8c |
|
84ad31ff |
st = avformat_new_stream(s, NULL); |
28146a8c |
if(!st) |
769e10f0 |
return AVERROR(ENOMEM); |
28146a8c |
|
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE); |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
28146a8c |
st->codec->codec_id = CODEC_ID_MP3;
st->codec->bit_rate = mtv->audio_br; |
57004ff1 |
st->need_parsing = AVSTREAM_PARSE_FULL; |
28146a8c |
|
a45bdbc4 |
// Jump over header |
28146a8c |
|
6b4aa5da |
if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE) |
6f3e0b21 |
return AVERROR(EIO); |
28146a8c |
|
ccd425e7 |
return 0; |
28146a8c |
}
static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
MTVDemuxContext *mtv = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
28146a8c |
int ret; |
63613fe6 |
#if !HAVE_BIGENDIAN |
28146a8c |
int i;
#endif
|
a2704c97 |
if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size) |
28146a8c |
{ |
45a8a02a |
avio_skip(pb, MTV_AUDIO_PADDING_SIZE); |
28146a8c |
ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE); |
5eef7bcd |
if(ret < 0)
return ret; |
28146a8c |
|
34bfe875 |
pkt->pos -= MTV_AUDIO_PADDING_SIZE; |
84ad31ff |
pkt->stream_index = 1; |
28146a8c |
}else
{
ret = av_get_packet(pb, pkt, mtv->img_segment_size); |
5eef7bcd |
if(ret < 0)
return ret; |
28146a8c |
|
63613fe6 |
#if !HAVE_BIGENDIAN |
28146a8c |
/* pkt->data is GGGRRRR BBBBBGGG
* and we need RRRRRGGG GGGBBBBB
* for PIX_FMT_RGB565 so here we
* just swap bytes as they come
*/
|
f890cb94 |
for(i=0;i<ret/2;i++) |
8fc0162a |
*((uint16_t *)pkt->data+i) = av_bswap16(*((uint16_t *)pkt->data+i)); |
28146a8c |
#endif |
84ad31ff |
pkt->stream_index = 0; |
28146a8c |
}
|
ccd425e7 |
return ret; |
28146a8c |
}
|
c6610a21 |
AVInputFormat ff_mtv_demuxer = { |
dfc2c4d9 |
.name = "MTV",
.long_name = NULL_IF_CONFIG_SMALL("MTV format"),
.priv_data_size = sizeof(MTVDemuxContext),
.read_probe = mtv_probe,
.read_header = mtv_read_header,
.read_packet = mtv_read_packet, |
28146a8c |
}; |