Mercurial > libavformat.hg
annotate mtv.c @ 5708:f096d96c7e5c libavformat
asfdec: skip byte array tags.
Patch from Anton Khirnov wyskas gmail
author | benoit |
---|---|
date | Wed, 24 Feb 2010 07:34:12 +0000 |
parents | b596cf9ab248 |
children | 0f6f55a8e878 |
rev | line source |
---|---|
1380 | 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 /** | |
4331
49c1d3b27727
Use full internal pathname in doxygen @file directives.
diego
parents:
4069
diff
changeset
|
23 * @file libavformat/mtv.c |
1380 | 24 * MTV demuxer. |
25 */ | |
26 | |
3286 | 27 #include "libavutil/bswap.h" |
5302 | 28 #include "libavutil/intreadwrite.h" |
1380 | 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 | |
4069 | 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 | |
5344 | 43 unsigned int audio_br; ///< bitrate of audio channel (mp3) |
4069 | 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; | |
1380 | 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 | |
5302 | 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 | |
5309
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
64 /* If width or height are 0 then imagesize header field should not */ |
5310 | 65 if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54])) |
5309
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
66 { |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
67 if(!!AV_RL16(&p->buf[56])) |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
68 return AVPROBE_SCORE_MAX/2; |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
69 else |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
70 return 0; |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
71 } |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
72 |
5343 | 73 if(p->buf[51] != 16) |
5344 | 74 return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway .. |
5343 | 75 |
1380 | 76 return AVPROBE_SCORE_MAX; |
77 } | |
78 | |
79 static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
80 { | |
4069 | 81 MTVDemuxContext *mtv = s->priv_data; |
82 ByteIOContext *pb = s->pb; | |
83 AVStream *st; | |
84 unsigned int audio_subsegments; | |
1380 | 85 |
86 url_fskip(pb, 3); | |
87 mtv->file_size = get_le32(pb); | |
88 mtv->segments = get_le32(pb); | |
89 url_fskip(pb, 32); | |
90 mtv->audio_identifier = get_le24(pb); | |
91 mtv->audio_br = get_le16(pb); | |
92 mtv->img_colorfmt = get_le24(pb); | |
93 mtv->img_bpp = get_byte(pb); | |
94 mtv->img_width = get_le16(pb); | |
95 mtv->img_height = get_le16(pb); | |
96 mtv->img_segment_size = get_le16(pb); | |
5309
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
97 |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
98 /* Calculate width and height if missing from header */ |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
99 |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
100 if(!mtv->img_width) |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
101 mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3) |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
102 / mtv->img_height; |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
103 |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
104 if(!mtv->img_height) |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
105 mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3) |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
106 / mtv->img_width; |
a011470132ca
If missing, calculate width or height from bpp and
reynaldo
parents:
5302
diff
changeset
|
107 |
1380 | 108 url_fskip(pb, 4); |
3532
a741556d452c
Change mtv_read_packet so it does not break after seeking (displaying a shifted image).
reimar
parents:
3531
diff
changeset
|
109 audio_subsegments = get_le16(pb); |
a741556d452c
Change mtv_read_packet so it does not break after seeking (displaying a shifted image).
reimar
parents:
3531
diff
changeset
|
110 mtv->full_segment_size = |
a741556d452c
Change mtv_read_packet so it does not break after seeking (displaying a shifted image).
reimar
parents:
3531
diff
changeset
|
111 audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) + |
a741556d452c
Change mtv_read_packet so it does not break after seeking (displaying a shifted image).
reimar
parents:
3531
diff
changeset
|
112 mtv->img_segment_size; |
a741556d452c
Change mtv_read_packet so it does not break after seeking (displaying a shifted image).
reimar
parents:
3531
diff
changeset
|
113 mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments; |
1380 | 114 |
4043 | 115 // FIXME Add sanity check here |
1380 | 116 |
4043 | 117 // all systems go! init decoders |
1380 | 118 |
4043 | 119 // video - raw rgb565 |
1380 | 120 |
121 st = av_new_stream(s, VIDEO_SID); | |
122 if(!st) | |
2273
7eb456c4ed8a
Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents:
2023
diff
changeset
|
123 return AVERROR(ENOMEM); |
1380 | 124 |
125 av_set_pts_info(st, 64, 1, mtv->video_fps); | |
126 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
127 st->codec->codec_id = CODEC_ID_RAWVIDEO; | |
1449
64a44155a525
now we set codec_tag, still have to figure out how to handle flipping
reynaldo
parents:
1380
diff
changeset
|
128 st->codec->codec_tag = MKTAG('R', 'G', 'B', mtv->img_bpp); |
1380 | 129 st->codec->width = mtv->img_width; |
130 st->codec->height = mtv->img_height; | |
3908
1d3d17de20ba
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
3532
diff
changeset
|
131 st->codec->bits_per_coded_sample = mtv->img_bpp; |
1380 | 132 st->codec->sample_rate = mtv->video_fps; |
5144
65f73f11261c
Mark video from .mtv files correctly as upside-down.
reimar
parents:
5108
diff
changeset
|
133 st->codec->extradata = av_strdup("BottomUp"); |
65f73f11261c
Mark video from .mtv files correctly as upside-down.
reimar
parents:
5108
diff
changeset
|
134 st->codec->extradata_size = 9; |
1380 | 135 |
4043 | 136 // audio - mp3 |
1380 | 137 |
138 st = av_new_stream(s, AUDIO_SID); | |
139 if(!st) | |
2273
7eb456c4ed8a
Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents:
2023
diff
changeset
|
140 return AVERROR(ENOMEM); |
1380 | 141 |
142 av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE); | |
143 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
144 st->codec->codec_id = CODEC_ID_MP3; | |
145 st->codec->bit_rate = mtv->audio_br; | |
2023 | 146 st->need_parsing = AVSTREAM_PARSE_FULL; |
1380 | 147 |
4043 | 148 // Jump over header |
1380 | 149 |
150 if(url_fseek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
151 return AVERROR(EIO); |
1380 | 152 |
3278 | 153 return 0; |
1380 | 154 |
155 } | |
156 | |
157 static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt) | |
158 { | |
159 MTVDemuxContext *mtv = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
160 ByteIOContext *pb = s->pb; |
1380 | 161 int ret; |
5108 | 162 #if !HAVE_BIGENDIAN |
1380 | 163 int i; |
164 #endif | |
165 | |
3532
a741556d452c
Change mtv_read_packet so it does not break after seeking (displaying a shifted image).
reimar
parents:
3531
diff
changeset
|
166 if((url_ftell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size) |
1380 | 167 { |
168 url_fskip(pb, MTV_AUDIO_PADDING_SIZE); | |
169 | |
170 ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE); | |
171 if(ret != MTV_ASUBCHUNK_DATA_SIZE) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
172 return AVERROR(EIO); |
1380 | 173 |
3531
296caaa22429
Fix pkt->pos to really point at start of packet for mtv audio packets.
reimar
parents:
3424
diff
changeset
|
174 pkt->pos -= MTV_AUDIO_PADDING_SIZE; |
1380 | 175 pkt->stream_index = AUDIO_SID; |
176 | |
177 }else | |
178 { | |
179 ret = av_get_packet(pb, pkt, mtv->img_segment_size); | |
180 if(ret != mtv->img_segment_size) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
181 return AVERROR(EIO); |
1380 | 182 |
5108 | 183 #if !HAVE_BIGENDIAN |
1380 | 184 |
185 /* pkt->data is GGGRRRR BBBBBGGG | |
186 * and we need RRRRRGGG GGGBBBBB | |
187 * for PIX_FMT_RGB565 so here we | |
188 * just swap bytes as they come | |
189 */ | |
190 | |
191 for(i=0;i<mtv->img_segment_size/2;i++) | |
192 *((uint16_t *)pkt->data+i) = bswap_16(*((uint16_t *)pkt->data+i)); | |
193 #endif | |
194 pkt->stream_index = VIDEO_SID; | |
195 } | |
196 | |
3278 | 197 return ret; |
1380 | 198 } |
199 | |
200 AVInputFormat mtv_demuxer = { | |
201 "MTV", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3286
diff
changeset
|
202 NULL_IF_CONFIG_SMALL("MTV format"), |
1380 | 203 sizeof(MTVDemuxContext), |
204 mtv_probe, | |
205 mtv_read_header, | |
206 mtv_read_packet, | |
207 }; |