]> git.sesse.net Git - ffmpeg/blob - libavformat/mtv.c
build: fix standalone compilation of OMA muxer
[ffmpeg] / libavformat / mtv.c
1 /*
2  * mtv demuxer
3  * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 #include "internal.h"
31
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
36
37 typedef struct MTVDemuxContext {
38
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;
50
51 } MTVDemuxContext;
52
53 static int mtv_probe(AVProbeData *p)
54 {
55     /* Magic is 'AMV' */
56     if(*(p->buf) != 'A' || *(p->buf+1) != 'M' || *(p->buf+2) != 'V')
57         return 0;
58
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])))
61         return 0;
62
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]))
65     {
66         if(!!AV_RL16(&p->buf[56]))
67             return AVPROBE_SCORE_MAX/2;
68         else
69             return 0;
70     }
71
72     if(p->buf[51] != 16)
73         return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway ..
74
75     return AVPROBE_SCORE_MAX;
76 }
77
78 static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
79 {
80     MTVDemuxContext *mtv = s->priv_data;
81     AVIOContext   *pb  = s->pb;
82     AVStream        *st;
83     unsigned int    audio_subsegments;
84
85     avio_skip(pb, 3);
86     mtv->file_size         = avio_rl32(pb);
87     mtv->segments          = avio_rl32(pb);
88     avio_skip(pb, 32);
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);
96
97     /* Calculate width and height if missing from header */
98
99     if(!mtv->img_width)
100         mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
101                         / mtv->img_height;
102
103     if(!mtv->img_height)
104         mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
105                         / mtv->img_width;
106
107     avio_skip(pb, 4);
108     audio_subsegments = avio_rl16(pb);
109     mtv->full_segment_size =
110         audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
111         mtv->img_segment_size;
112     mtv->video_fps         = (mtv->audio_br / 4) / audio_subsegments;
113
114     // FIXME Add sanity check here
115
116     // all systems go! init decoders
117
118     // video - raw rgb565
119
120     st = avformat_new_stream(s, NULL);
121     if(!st)
122         return AVERROR(ENOMEM);
123
124     avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
125     st->codec->codec_type      = AVMEDIA_TYPE_VIDEO;
126     st->codec->codec_id        = CODEC_ID_RAWVIDEO;
127     st->codec->pix_fmt         = PIX_FMT_RGB565;
128     st->codec->width           = mtv->img_width;
129     st->codec->height          = mtv->img_height;
130     st->codec->sample_rate     = mtv->video_fps;
131     st->codec->extradata       = av_strdup("BottomUp");
132     st->codec->extradata_size  = 9;
133
134     // audio - mp3
135
136     st = avformat_new_stream(s, NULL);
137     if(!st)
138         return AVERROR(ENOMEM);
139
140     avpriv_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
141     st->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
142     st->codec->codec_id        = CODEC_ID_MP3;
143     st->codec->bit_rate        = mtv->audio_br;
144     st->need_parsing           = AVSTREAM_PARSE_FULL;
145
146     // Jump over header
147
148     if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
149         return AVERROR(EIO);
150
151     return 0;
152
153 }
154
155 static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
156 {
157     MTVDemuxContext *mtv = s->priv_data;
158     AVIOContext *pb = s->pb;
159     int ret;
160 #if !HAVE_BIGENDIAN
161     int i;
162 #endif
163
164     if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
165     {
166         avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
167
168         ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
169         if(ret < 0)
170             return ret;
171
172         pkt->pos -= MTV_AUDIO_PADDING_SIZE;
173         pkt->stream_index = 1;
174
175     }else
176     {
177         ret = av_get_packet(pb, pkt, mtv->img_segment_size);
178         if(ret < 0)
179             return ret;
180
181 #if !HAVE_BIGENDIAN
182
183         /* pkt->data is GGGRRRR BBBBBGGG
184          * and we need RRRRRGGG GGGBBBBB
185          * for PIX_FMT_RGB565 so here we
186          * just swap bytes as they come
187          */
188
189         for(i=0;i<mtv->img_segment_size/2;i++)
190             *((uint16_t *)pkt->data+i) = av_bswap16(*((uint16_t *)pkt->data+i));
191 #endif
192         pkt->stream_index = 0;
193     }
194
195     return ret;
196 }
197
198 AVInputFormat ff_mtv_demuxer = {
199     .name           = "MTV",
200     .long_name      = NULL_IF_CONFIG_SMALL("MTV format"),
201     .priv_data_size = sizeof(MTVDemuxContext),
202     .read_probe     = mtv_probe,
203     .read_header    = mtv_read_header,
204     .read_packet    = mtv_read_packet,
205 };