Statistics
| Branch: | Revision:

ffmpeg / libavformat / mtv.c @ f122ef36

History | View | Annotate | Download (5.42 KB)

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 libavformat/mtv.c
24
 * MTV demuxer.
25
 */
26

    
27
#include "libavutil/bswap.h"
28
#include "libavutil/intreadwrite.h"
29
#include "avformat.h"
30

    
31
#define MTV_ASUBCHUNK_DATA_SIZE 500
32
#define MTV_HEADER_SIZE 512
33
#define MTV_AUDIO_PADDING_SIZE 12
34
#define AUDIO_SAMPLING_RATE 44100
35
#define VIDEO_SID 0
36
#define AUDIO_SID 1
37

    
38
typedef struct MTVDemuxContext {
39

    
40
    unsigned int file_size;         ///< filesize, not always right
41
    unsigned int segments;          ///< number of 512 byte segments
42
    unsigned int audio_identifier;  ///< 'MP3' on all files I have seen
43
    unsigned int audio_br;          ///< bitrate of audio chanel (mp3)
44
    unsigned int img_colorfmt;      ///< frame colorfmt rgb 565/555
45
    unsigned int img_bpp;           ///< frame bits per pixel
46
    unsigned int img_width;         //
47
    unsigned int img_height;        //
48
    unsigned int img_segment_size;  ///< size of image segment
49
    unsigned int video_fps;         //
50
    unsigned int full_segment_size;
51

    
52
} MTVDemuxContext;
53

    
54
static int mtv_probe(AVProbeData *p)
55
{
56
    /* Magic is 'AMV' */
57
    if(*(p->buf) != 'A' || *(p->buf+1) != 'M' || *(p->buf+2) != 'V')
58
        return 0;
59

    
60
    /* Check for nonzero in bpp and (width|height) header fields */
61
    if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
62
        return 0;
63

    
64
    return AVPROBE_SCORE_MAX;
65
}
66

    
67
static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
68
{
69
    MTVDemuxContext *mtv = s->priv_data;
70
    ByteIOContext   *pb  = s->pb;
71
    AVStream        *st;
72
    unsigned int    audio_subsegments;
73

    
74
    url_fskip(pb, 3);
75
    mtv->file_size         = get_le32(pb);
76
    mtv->segments          = get_le32(pb);
77
    url_fskip(pb, 32);
78
    mtv->audio_identifier  = get_le24(pb);
79
    mtv->audio_br          = get_le16(pb);
80
    mtv->img_colorfmt      = get_le24(pb);
81
    mtv->img_bpp           = get_byte(pb);
82
    mtv->img_width         = get_le16(pb);
83
    mtv->img_height        = get_le16(pb);
84
    mtv->img_segment_size  = get_le16(pb);
85
    url_fskip(pb, 4);
86
    audio_subsegments = get_le16(pb);
87
    mtv->full_segment_size =
88
        audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
89
        mtv->img_segment_size;
90
    mtv->video_fps         = (mtv->audio_br / 4) / audio_subsegments;
91

    
92
    // FIXME Add sanity check here
93

    
94
    // all systems go! init decoders
95

    
96
    // video - raw rgb565
97

    
98
    st = av_new_stream(s, VIDEO_SID);
99
    if(!st)
100
        return AVERROR(ENOMEM);
101

    
102
    av_set_pts_info(st, 64, 1, mtv->video_fps);
103
    st->codec->codec_type      = CODEC_TYPE_VIDEO;
104
    st->codec->codec_id        = CODEC_ID_RAWVIDEO;
105
    st->codec->codec_tag       = MKTAG('R', 'G', 'B', mtv->img_bpp);
106
    st->codec->width           = mtv->img_width;
107
    st->codec->height          = mtv->img_height;
108
    st->codec->bits_per_coded_sample = mtv->img_bpp;
109
    st->codec->sample_rate     = mtv->video_fps;
110
    st->codec->extradata       = av_strdup("BottomUp");
111
    st->codec->extradata_size  = 9;
112

    
113
    // audio - mp3
114

    
115
    st = av_new_stream(s, AUDIO_SID);
116
    if(!st)
117
        return AVERROR(ENOMEM);
118

    
119
    av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
120
    st->codec->codec_type      = CODEC_TYPE_AUDIO;
121
    st->codec->codec_id        = CODEC_ID_MP3;
122
    st->codec->bit_rate        = mtv->audio_br;
123
    st->need_parsing           = AVSTREAM_PARSE_FULL;
124

    
125
    // Jump over header
126

    
127
    if(url_fseek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
128
        return AVERROR(EIO);
129

    
130
    return 0;
131

    
132
}
133

    
134
static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
135
{
136
    MTVDemuxContext *mtv = s->priv_data;
137
    ByteIOContext *pb = s->pb;
138
    int ret;
139
#if !HAVE_BIGENDIAN
140
    int i;
141
#endif
142

    
143
    if((url_ftell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
144
    {
145
        url_fskip(pb, MTV_AUDIO_PADDING_SIZE);
146

    
147
        ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
148
        if(ret != MTV_ASUBCHUNK_DATA_SIZE)
149
            return AVERROR(EIO);
150

    
151
        pkt->pos -= MTV_AUDIO_PADDING_SIZE;
152
        pkt->stream_index = AUDIO_SID;
153

    
154
    }else
155
    {
156
        ret = av_get_packet(pb, pkt, mtv->img_segment_size);
157
        if(ret != mtv->img_segment_size)
158
            return AVERROR(EIO);
159

    
160
#if !HAVE_BIGENDIAN
161

    
162
        /* pkt->data is GGGRRRR BBBBBGGG
163
         * and we need RRRRRGGG GGGBBBBB
164
         * for PIX_FMT_RGB565 so here we
165
         * just swap bytes as they come
166
         */
167

    
168
        for(i=0;i<mtv->img_segment_size/2;i++)
169
            *((uint16_t *)pkt->data+i) = bswap_16(*((uint16_t *)pkt->data+i));
170
#endif
171
        pkt->stream_index = VIDEO_SID;
172
    }
173

    
174
    return ret;
175
}
176

    
177
AVInputFormat mtv_demuxer = {
178
    "MTV",
179
    NULL_IF_CONFIG_SMALL("MTV format"),
180
    sizeof(MTVDemuxContext),
181
    mtv_probe,
182
    mtv_read_header,
183
    mtv_read_packet,
184
};