Statistics
| Branch: | Revision:

ffmpeg / libavformat / mtv.c @ e356fc57

History | View | Annotate | Download (6 KB)

1 28146a8c Reynaldo H. Verdejo Pinochet
/*
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 ba87f080 Diego Biurrun
 * @file
24 28146a8c Reynaldo H. Verdejo Pinochet
 * MTV demuxer.
25
 */
26
27 245976da Diego Biurrun
#include "libavutil/bswap.h"
28 f122ef36 Reynaldo H. Verdejo Pinochet
#include "libavutil/intreadwrite.h"
29 28146a8c Reynaldo H. Verdejo Pinochet
#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 7100376f Reynaldo H. Verdejo Pinochet
    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 87e76ae0 Reynaldo H. Verdejo Pinochet
    unsigned int audio_br;          ///< bitrate of audio channel (mp3)
44 7100376f Reynaldo H. Verdejo Pinochet
    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 28146a8c Reynaldo H. Verdejo Pinochet
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 f122ef36 Reynaldo H. Verdejo Pinochet
    /* 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 9cf6bbb9 Reynaldo H. Verdejo Pinochet
    /* If width or height are 0 then imagesize header field should not */
65 cbd8a722 Reynaldo H. Verdejo Pinochet
    if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
66 9cf6bbb9 Reynaldo H. Verdejo Pinochet
    {
67
        if(!!AV_RL16(&p->buf[56]))
68
            return AVPROBE_SCORE_MAX/2;
69
        else
70
            return 0;
71
    }
72
73 a8e1cbaf Reynaldo H. Verdejo Pinochet
    if(p->buf[51] != 16)
74 87e76ae0 Reynaldo H. Verdejo Pinochet
        return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway ..
75 a8e1cbaf Reynaldo H. Verdejo Pinochet
76 28146a8c Reynaldo H. Verdejo Pinochet
    return AVPROBE_SCORE_MAX;
77
}
78
79
static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
80
{
81 7100376f Reynaldo H. Verdejo Pinochet
    MTVDemuxContext *mtv = s->priv_data;
82 ae628ec1 Anton Khirnov
    AVIOContext   *pb  = s->pb;
83 7100376f Reynaldo H. Verdejo Pinochet
    AVStream        *st;
84
    unsigned int    audio_subsegments;
85 28146a8c Reynaldo H. Verdejo Pinochet
86 e356fc57 Anton Khirnov
    avio_seek(pb, 3, SEEK_CUR);
87 b7effd4e Anton Khirnov
    mtv->file_size         = avio_rl32(pb);
88
    mtv->segments          = avio_rl32(pb);
89 e356fc57 Anton Khirnov
    avio_seek(pb, 32, SEEK_CUR);
90 b7effd4e Anton Khirnov
    mtv->audio_identifier  = avio_rl24(pb);
91
    mtv->audio_br          = avio_rl16(pb);
92
    mtv->img_colorfmt      = avio_rl24(pb);
93
    mtv->img_bpp           = avio_r8(pb);
94
    mtv->img_width         = avio_rl16(pb);
95
    mtv->img_height        = avio_rl16(pb);
96
    mtv->img_segment_size  = avio_rl16(pb);
97 9cf6bbb9 Reynaldo H. Verdejo Pinochet
98
    /* Calculate width and height if missing from header */
99
100
    if(!mtv->img_width)
101
        mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
102
                        / mtv->img_height;
103
104
    if(!mtv->img_height)
105
        mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
106
                        / mtv->img_width;
107
108 e356fc57 Anton Khirnov
    avio_seek(pb, 4, SEEK_CUR);
109 b7effd4e Anton Khirnov
    audio_subsegments = avio_rl16(pb);
110 ddca6ab6 Reimar Döffinger
    mtv->full_segment_size =
111
        audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
112
        mtv->img_segment_size;
113
    mtv->video_fps         = (mtv->audio_br / 4) / audio_subsegments;
114 28146a8c Reynaldo H. Verdejo Pinochet
115 a45bdbc4 Reynaldo H. Verdejo Pinochet
    // FIXME Add sanity check here
116 28146a8c Reynaldo H. Verdejo Pinochet
117 a45bdbc4 Reynaldo H. Verdejo Pinochet
    // all systems go! init decoders
118 28146a8c Reynaldo H. Verdejo Pinochet
119 a45bdbc4 Reynaldo H. Verdejo Pinochet
    // video - raw rgb565
120 28146a8c Reynaldo H. Verdejo Pinochet
121
    st = av_new_stream(s, VIDEO_SID);
122
    if(!st)
123 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
124 28146a8c Reynaldo H. Verdejo Pinochet
125
    av_set_pts_info(st, 64, 1, mtv->video_fps);
126 72415b2a Stefano Sabatini
    st->codec->codec_type      = AVMEDIA_TYPE_VIDEO;
127 28146a8c Reynaldo H. Verdejo Pinochet
    st->codec->codec_id        = CODEC_ID_RAWVIDEO;
128 10fa22cc Vitor Sessak
    st->codec->pix_fmt         = PIX_FMT_RGB565;
129 28146a8c Reynaldo H. Verdejo Pinochet
    st->codec->width           = mtv->img_width;
130
    st->codec->height          = mtv->img_height;
131
    st->codec->sample_rate     = mtv->video_fps;
132 49056037 Reimar Döffinger
    st->codec->extradata       = av_strdup("BottomUp");
133
    st->codec->extradata_size  = 9;
134 28146a8c Reynaldo H. Verdejo Pinochet
135 a45bdbc4 Reynaldo H. Verdejo Pinochet
    // audio - mp3
136 28146a8c Reynaldo H. Verdejo Pinochet
137
    st = av_new_stream(s, AUDIO_SID);
138
    if(!st)
139 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
140 28146a8c Reynaldo H. Verdejo Pinochet
141
    av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
142 72415b2a Stefano Sabatini
    st->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
143 28146a8c Reynaldo H. Verdejo Pinochet
    st->codec->codec_id        = CODEC_ID_MP3;
144
    st->codec->bit_rate        = mtv->audio_br;
145 57004ff1 Aurelien Jacobs
    st->need_parsing           = AVSTREAM_PARSE_FULL;
146 28146a8c Reynaldo H. Verdejo Pinochet
147 a45bdbc4 Reynaldo H. Verdejo Pinochet
    // Jump over header
148 28146a8c Reynaldo H. Verdejo Pinochet
149 6b4aa5da Anton Khirnov
    if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
150 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
151 28146a8c Reynaldo H. Verdejo Pinochet
152 ccd425e7 Diego Biurrun
    return 0;
153 28146a8c Reynaldo H. Verdejo Pinochet
154
}
155
156
static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
157
{
158
    MTVDemuxContext *mtv = s->priv_data;
159 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
160 28146a8c Reynaldo H. Verdejo Pinochet
    int ret;
161 63613fe6 Måns Rullgård
#if !HAVE_BIGENDIAN
162 28146a8c Reynaldo H. Verdejo Pinochet
    int i;
163
#endif
164
165 ddca6ab6 Reimar Döffinger
    if((url_ftell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
166 28146a8c Reynaldo H. Verdejo Pinochet
    {
167 e356fc57 Anton Khirnov
        avio_seek(pb, MTV_AUDIO_PADDING_SIZE, SEEK_CUR);
168 28146a8c Reynaldo H. Verdejo Pinochet
169
        ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
170 5eef7bcd Vitor Sessak
        if(ret < 0)
171
            return ret;
172 28146a8c Reynaldo H. Verdejo Pinochet
173 34bfe875 Reimar Döffinger
        pkt->pos -= MTV_AUDIO_PADDING_SIZE;
174 28146a8c Reynaldo H. Verdejo Pinochet
        pkt->stream_index = AUDIO_SID;
175
176
    }else
177
    {
178
        ret = av_get_packet(pb, pkt, mtv->img_segment_size);
179 5eef7bcd Vitor Sessak
        if(ret < 0)
180
            return ret;
181 28146a8c Reynaldo H. Verdejo Pinochet
182 63613fe6 Måns Rullgård
#if !HAVE_BIGENDIAN
183 28146a8c Reynaldo H. Verdejo Pinochet
184
        /* pkt->data is GGGRRRR BBBBBGGG
185
         * and we need RRRRRGGG GGGBBBBB
186
         * for PIX_FMT_RGB565 so here we
187
         * just swap bytes as they come
188
         */
189
190
        for(i=0;i<mtv->img_segment_size/2;i++)
191 8fc0162a Måns Rullgård
            *((uint16_t *)pkt->data+i) = av_bswap16(*((uint16_t *)pkt->data+i));
192 28146a8c Reynaldo H. Verdejo Pinochet
#endif
193
        pkt->stream_index = VIDEO_SID;
194
    }
195
196 ccd425e7 Diego Biurrun
    return ret;
197 28146a8c Reynaldo H. Verdejo Pinochet
}
198
199 c6610a21 Diego Elio Pettenò
AVInputFormat ff_mtv_demuxer = {
200 28146a8c Reynaldo H. Verdejo Pinochet
    "MTV",
201 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MTV format"),
202 28146a8c Reynaldo H. Verdejo Pinochet
    sizeof(MTVDemuxContext),
203
    mtv_probe,
204
    mtv_read_header,
205
    mtv_read_packet,
206
};