Mercurial > libavformat.hg
annotate avidec.c @ 13:8a5285a0ca2f libavformat
Fix for odd strf tag in Stargate SG-1 - 3x18 - Shades of Grey.avi
| author | mmu_man |
|---|---|
| date | Fri, 20 Dec 2002 21:05:59 +0000 |
| parents | 39c4c4336486 |
| children | 2036f790ece3 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * AVI decoder. | |
| 3 * Copyright (c) 2001 Fabrice Bellard. | |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Lesser General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 * Lesser General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Lesser General Public | |
| 16 * License along with this library; if not, write to the Free Software | |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 */ | |
| 19 #include "avformat.h" | |
| 20 #include "avi.h" | |
| 21 | |
| 22 //#define DEBUG | |
| 23 | |
| 24 typedef struct AVIIndex { | |
| 25 unsigned char tag[4]; | |
| 26 unsigned int flags, pos, len; | |
| 27 struct AVIIndex *next; | |
| 28 } AVIIndex; | |
| 29 | |
| 30 typedef struct { | |
| 31 INT64 movi_end; | |
| 32 offset_t movi_list; | |
| 33 AVIIndex *first, *last; | |
| 34 } AVIContext; | |
| 35 | |
| 36 #ifdef DEBUG | |
| 37 static void print_tag(const char *str, unsigned int tag, int size) | |
| 38 { | |
| 39 printf("%s: tag=%c%c%c%c size=0x%x\n", | |
| 40 str, tag & 0xff, | |
| 41 (tag >> 8) & 0xff, | |
| 42 (tag >> 16) & 0xff, | |
| 43 (tag >> 24) & 0xff, | |
| 44 size); | |
| 45 } | |
| 46 #endif | |
| 47 | |
| 48 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
| 49 { | |
| 50 AVIContext *avi = s->priv_data; | |
| 51 ByteIOContext *pb = &s->pb; | |
| 52 UINT32 tag, tag1; | |
| 53 int codec_type, stream_index, size, frame_period, bit_rate; | |
| 54 int i; | |
| 55 AVStream *st; | |
| 56 | |
| 57 /* check RIFF header */ | |
| 58 tag = get_le32(pb); | |
| 59 | |
| 60 if (tag != MKTAG('R', 'I', 'F', 'F')) | |
| 61 return -1; | |
| 62 get_le32(pb); /* file size */ | |
| 63 tag = get_le32(pb); | |
| 64 if (tag != MKTAG('A', 'V', 'I', ' ')) | |
| 65 return -1; | |
| 66 | |
| 67 /* first list tag */ | |
| 68 stream_index = -1; | |
| 69 codec_type = -1; | |
| 70 frame_period = 0; | |
| 71 for(;;) { | |
| 72 if (url_feof(pb)) | |
| 73 goto fail; | |
| 74 tag = get_le32(pb); | |
| 75 size = get_le32(pb); | |
| 76 #ifdef DEBUG | |
| 77 print_tag("tag", tag, size); | |
| 78 #endif | |
| 79 | |
| 80 switch(tag) { | |
| 81 case MKTAG('L', 'I', 'S', 'T'): | |
| 82 /* ignored, except when start of video packets */ | |
| 83 tag1 = get_le32(pb); | |
| 84 #ifdef DEBUG | |
| 85 print_tag("list", tag1, 0); | |
| 86 #endif | |
| 87 if (tag1 == MKTAG('m', 'o', 'v', 'i')) { | |
| 88 avi->movi_end = url_ftell(pb) + size - 4; | |
| 89 #ifdef DEBUG | |
| 90 printf("movi end=%Lx\n", avi->movi_end); | |
| 91 #endif | |
| 92 goto end_of_header; | |
| 93 } | |
| 94 break; | |
| 95 case MKTAG('a', 'v', 'i', 'h'): | |
| 96 /* avi header */ | |
| 97 /* using frame_period is bad idea */ | |
| 98 frame_period = get_le32(pb); | |
| 99 bit_rate = get_le32(pb) * 8; | |
| 100 url_fskip(pb, 4 * 4); | |
| 101 s->nb_streams = get_le32(pb); | |
| 102 for(i=0;i<s->nb_streams;i++) { | |
| 103 AVStream *st = av_mallocz(sizeof(AVStream)); | |
| 104 if (!st) | |
| 105 goto fail; | |
| 5 | 106 avcodec_get_context_defaults(&st->codec); |
| 107 | |
| 0 | 108 s->streams[i] = st; |
| 109 } | |
| 110 url_fskip(pb, size - 7 * 4); | |
| 111 break; | |
| 112 case MKTAG('s', 't', 'r', 'h'): | |
| 113 /* stream header */ | |
| 114 stream_index++; | |
| 115 tag1 = get_le32(pb); | |
| 116 switch(tag1) { | |
| 117 case MKTAG('v', 'i', 'd', 's'): | |
| 118 codec_type = CODEC_TYPE_VIDEO; | |
| 119 get_le32(pb); /* codec tag */ | |
| 120 get_le32(pb); /* flags */ | |
| 121 get_le16(pb); /* priority */ | |
| 122 get_le16(pb); /* language */ | |
| 123 get_le32(pb); /* XXX: initial frame ? */ | |
| 124 get_le32(pb); /* scale */ | |
| 125 get_le32(pb); /* rate */ | |
| 126 size -= 6 * 4; | |
| 127 break; | |
| 128 case MKTAG('a', 'u', 'd', 's'): | |
| 129 codec_type = CODEC_TYPE_AUDIO; | |
| 130 /* nothing really useful */ | |
| 131 } | |
| 132 url_fskip(pb, size - 4); | |
| 133 break; | |
| 134 case MKTAG('s', 't', 'r', 'f'): | |
| 135 /* stream header */ | |
| 136 if (stream_index >= s->nb_streams) { | |
| 137 url_fskip(pb, size); | |
| 138 } else { | |
| 139 st = s->streams[stream_index]; | |
| 140 switch(codec_type) { | |
| 141 case CODEC_TYPE_VIDEO: | |
| 142 get_le32(pb); /* size */ | |
| 143 st->codec.width = get_le32(pb); | |
| 144 st->codec.height = get_le32(pb); | |
| 145 if (frame_period) | |
| 146 st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period; | |
| 147 else | |
| 148 st->codec.frame_rate = 25 * FRAME_RATE_BASE; | |
| 149 get_le16(pb); /* panes */ | |
| 150 get_le16(pb); /* depth */ | |
| 151 tag1 = get_le32(pb); | |
| 152 #ifdef DEBUG | |
| 153 print_tag("video", tag1, 0); | |
| 154 #endif | |
| 155 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
| 156 st->codec.codec_tag = tag1; | |
| 157 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1); | |
| 158 url_fskip(pb, size - 5 * 4); | |
| 159 break; | |
| 160 case CODEC_TYPE_AUDIO: | |
| 161 get_wav_header(pb, &st->codec, (size >= 18)); | |
|
13
8a5285a0ca2f
Fix for odd strf tag in Stargate SG-1 - 3x18 - Shades of Grey.avi
mmu_man
parents:
5
diff
changeset
|
162 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */ |
|
8a5285a0ca2f
Fix for odd strf tag in Stargate SG-1 - 3x18 - Shades of Grey.avi
mmu_man
parents:
5
diff
changeset
|
163 url_fskip(pb, 1); |
| 0 | 164 break; |
| 165 default: | |
| 166 url_fskip(pb, size); | |
| 167 break; | |
| 168 } | |
| 169 } | |
| 170 break; | |
| 171 default: | |
| 172 /* skip tag */ | |
| 173 size += (size & 1); | |
| 174 url_fskip(pb, size); | |
| 175 break; | |
| 176 } | |
| 177 } | |
| 178 end_of_header: | |
| 179 /* check stream number */ | |
| 180 if (stream_index != s->nb_streams - 1) { | |
| 181 fail: | |
| 182 for(i=0;i<s->nb_streams;i++) { | |
| 183 av_freep(&s->streams[i]); | |
| 184 } | |
| 185 return -1; | |
| 186 } | |
| 187 | |
| 188 return 0; | |
| 189 } | |
| 190 | |
| 191 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 192 { | |
| 193 AVIContext *avi = s->priv_data; | |
| 194 ByteIOContext *pb = &s->pb; | |
| 195 int n, d1, d2, size; | |
| 196 | |
| 197 for(;;) { | |
| 198 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end) | |
| 199 return -1; | |
| 200 d1 = get_byte(pb) - '0'; | |
| 201 d2 = get_byte(pb) - '0'; | |
| 202 if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9) | |
| 203 continue; | |
| 204 | |
| 205 n = d1 * 10 + d2; | |
| 206 if (n < 0 || n >= s->nb_streams) | |
| 207 continue; | |
| 208 | |
| 209 d1 = get_byte(pb); | |
| 210 d2 = get_byte(pb); | |
| 211 if ((d1 == 'd' && d2 == 'c') | |
| 212 || (d1 == 'w' && d2 == 'b')) | |
| 213 break; | |
| 214 } | |
| 215 size = get_le32(pb); | |
| 216 av_new_packet(pkt, size); | |
| 217 pkt->stream_index = n; | |
| 218 | |
| 219 get_buffer(pb, pkt->data, pkt->size); | |
| 220 | |
| 221 if (size & 1) | |
| 222 get_byte(pb); | |
| 223 | |
| 224 return 0; | |
| 225 } | |
| 226 | |
| 227 static int avi_read_close(AVFormatContext *s) | |
| 228 { | |
| 229 return 0; | |
| 230 } | |
| 231 | |
| 232 static int avi_probe(AVProbeData *p) | |
| 233 { | |
| 234 /* check file header */ | |
| 235 if (p->buf_size <= 32) | |
| 236 return 0; | |
| 237 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
| 238 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
| 239 p->buf[8] == 'A' && p->buf[9] == 'V' && | |
| 240 p->buf[10] == 'I' && p->buf[11] == ' ') | |
| 241 return AVPROBE_SCORE_MAX; | |
| 242 else | |
| 243 return 0; | |
| 244 } | |
| 245 | |
| 246 static AVInputFormat avi_iformat = { | |
| 247 "avi", | |
| 248 "avi format", | |
| 249 sizeof(AVIContext), | |
| 250 avi_probe, | |
| 251 avi_read_header, | |
| 252 avi_read_packet, | |
| 253 avi_read_close, | |
| 254 }; | |
| 255 | |
| 256 int avidec_init(void) | |
| 257 { | |
| 258 av_register_input_format(&avi_iformat); | |
| 259 return 0; | |
| 260 } |
