]> git.sesse.net Git - ffmpeg/blob - libavformat/mtv.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / mtv.c
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
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
36 typedef struct MTVDemuxContext {
37
38     unsigned int file_size;         ///< filesize, not always right
39     unsigned int segments;          ///< number of 512 byte segments
40     unsigned int audio_identifier;  ///< 'MP3' on all files I have seen
41     unsigned int audio_br;          ///< bitrate of audio channel (mp3)
42     unsigned int img_colorfmt;      ///< frame colorfmt rgb 565/555
43     unsigned int img_bpp;           ///< frame bits per pixel
44     unsigned int img_width;         //
45     unsigned int img_height;        //
46     unsigned int img_segment_size;  ///< size of image segment
47     unsigned int video_fps;         //
48     unsigned int full_segment_size;
49
50 } MTVDemuxContext;
51
52 static int mtv_probe(AVProbeData *p)
53 {
54     /* Magic is 'AMV' */
55     if(*(p->buf) != 'A' || *(p->buf+1) != 'M' || *(p->buf+2) != 'V')
56         return 0;
57
58     /* Check for nonzero in bpp and (width|height) header fields */
59     if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
60         return 0;
61
62     /* If width or height are 0 then imagesize header field should not */
63     if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
64     {
65         if(!!AV_RL16(&p->buf[56]))
66             return AVPROBE_SCORE_MAX/2;
67         else
68             return 0;
69     }
70
71     if(p->buf[51] != 16)
72         return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway ..
73
74     return AVPROBE_SCORE_MAX;
75 }
76
77 static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
78 {
79     MTVDemuxContext *mtv = s->priv_data;
80     AVIOContext   *pb  = s->pb;
81     AVStream        *st;
82     unsigned int    audio_subsegments;
83
84     avio_skip(pb, 3);
85     mtv->file_size         = avio_rl32(pb);
86     mtv->segments          = avio_rl32(pb);
87     avio_skip(pb, 32);
88     mtv->audio_identifier  = avio_rl24(pb);
89     mtv->audio_br          = avio_rl16(pb);
90     mtv->img_colorfmt      = avio_rl24(pb);
91     mtv->img_bpp           = avio_r8(pb);
92     mtv->img_width         = avio_rl16(pb);
93     mtv->img_height        = avio_rl16(pb);
94     mtv->img_segment_size  = avio_rl16(pb);
95
96     /* Calculate width and height if missing from header */
97
98     if(!mtv->img_width)
99         mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
100                         / mtv->img_height;
101
102     if(!mtv->img_height)
103         mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
104                         / mtv->img_width;
105
106     avio_skip(pb, 4);
107     audio_subsegments = avio_rl16(pb);
108     mtv->full_segment_size =
109         audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
110         mtv->img_segment_size;
111     mtv->video_fps         = (mtv->audio_br / 4) / audio_subsegments;
112
113     // FIXME Add sanity check here
114
115     // all systems go! init decoders
116
117     // video - raw rgb565
118
119     st = avformat_new_stream(s, NULL);
120     if(!st)
121         return AVERROR(ENOMEM);
122
123     av_set_pts_info(st, 64, 1, mtv->video_fps);
124     st->codec->codec_type      = AVMEDIA_TYPE_VIDEO;
125     st->codec->codec_id        = CODEC_ID_RAWVIDEO;
126     st->codec->pix_fmt         = PIX_FMT_RGB565;
127     st->codec->width           = mtv->img_width;
128     st->codec->height          = mtv->img_height;
129     st->codec->sample_rate     = mtv->video_fps;
130     st->codec->extradata       = av_strdup("BottomUp");
131     st->codec->extradata_size  = 9;
132
133     // audio - mp3
134
135     st = avformat_new_stream(s, NULL);
136     if(!st)
137         return AVERROR(ENOMEM);
138
139     av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
140     st->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
141     st->codec->codec_id        = CODEC_ID_MP3;
142     st->codec->bit_rate        = mtv->audio_br;
143     st->need_parsing           = AVSTREAM_PARSE_FULL;
144
145     // Jump over header
146
147     if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
148         return AVERROR(EIO);
149
150     return 0;
151
152 }
153
154 static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
155 {
156     MTVDemuxContext *mtv = s->priv_data;
157     AVIOContext *pb = s->pb;
158     int ret;
159 #if !HAVE_BIGENDIAN
160     int i;
161 #endif
162
163     if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
164     {
165         avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
166
167         ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
168         if(ret < 0)
169             return ret;
170
171         pkt->pos -= MTV_AUDIO_PADDING_SIZE;
172         pkt->stream_index = 1;
173
174     }else
175     {
176         ret = av_get_packet(pb, pkt, mtv->img_segment_size);
177         if(ret < 0)
178             return ret;
179
180 #if !HAVE_BIGENDIAN
181
182         /* pkt->data is GGGRRRR BBBBBGGG
183          * and we need RRRRRGGG GGGBBBBB
184          * for PIX_FMT_RGB565 so here we
185          * just swap bytes as they come
186          */
187
188         for(i=0;i<mtv->img_segment_size/2;i++)
189             *((uint16_t *)pkt->data+i) = av_bswap16(*((uint16_t *)pkt->data+i));
190 #endif
191         pkt->stream_index = 0;
192     }
193
194     return ret;
195 }
196
197 AVInputFormat ff_mtv_demuxer = {
198     .name           = "MTV",
199     .long_name      = NULL_IF_CONFIG_SMALL("MTV format"),
200     .priv_data_size = sizeof(MTVDemuxContext),
201     .read_probe     = mtv_probe,
202     .read_header    = mtv_read_header,
203     .read_packet    = mtv_read_packet,
204 };