Mercurial > libavformat.hg
comparison dxa.c @ 1918:7ccf23e42481 libavformat
DXA demuxer and decoder
author | kostya |
---|---|
date | Wed, 14 Mar 2007 14:49:49 +0000 |
parents | |
children | 1a3c9056982a |
comparison
equal
deleted
inserted
replaced
1917:6901a6e6dd95 | 1918:7ccf23e42481 |
---|---|
1 /* | |
2 * DXA demuxer | |
3 * Copyright (c) 2007 Konstantin Shishkov. | |
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 #include "avformat.h" | |
23 #include "riff.h" | |
24 | |
25 #define DXA_EXTRA_SIZE 9 | |
26 | |
27 typedef struct{ | |
28 int frames; | |
29 int has_sound; | |
30 int bpc; | |
31 uint32_t bytes_left; | |
32 int64_t wavpos, vidpos; | |
33 int readvid; | |
34 }DXAContext; | |
35 | |
36 static int dxa_probe(AVProbeData *p) | |
37 { | |
38 /* check file header */ | |
39 if (p->buf_size <= 4) | |
40 return 0; | |
41 if (p->buf[0] == 'D' && p->buf[1] == 'E' && | |
42 p->buf[2] == 'X' && p->buf[3] == 'A') | |
43 return AVPROBE_SCORE_MAX; | |
44 else | |
45 return 0; | |
46 } | |
47 | |
48 static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
49 { | |
50 ByteIOContext *pb = &s->pb; | |
51 DXAContext *c = s->priv_data; | |
52 AVStream *st, *ast; | |
53 uint32_t tag; | |
54 int32_t fps; | |
55 int w, h; | |
56 int num, den; | |
57 int flags; | |
58 | |
59 tag = get_le32(pb); | |
60 if (tag != MKTAG('D', 'E', 'X', 'A')) | |
61 return -1; | |
62 flags = get_byte(pb); | |
63 c->frames = get_be16(pb); | |
64 if(!c->frames){ | |
65 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n"); | |
66 return -1; | |
67 } | |
68 | |
69 fps = get_be32(pb); | |
70 if(fps > 0){ | |
71 den = 1000; | |
72 num = fps; | |
73 }else if (fps < 0){ | |
74 den = 100000; | |
75 num = -fps; | |
76 }else{ | |
77 den = 10; | |
78 num = 1; | |
79 } | |
80 w = get_be16(pb); | |
81 h = get_be16(pb); | |
82 c->has_sound = 0; | |
83 | |
84 st = av_new_stream(s, 0); | |
85 if (!st) | |
86 return -1; | |
87 | |
88 // Parse WAV data header | |
89 if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){ | |
90 uint32_t size, fsize; | |
91 c->has_sound = 1; | |
92 size = get_be32(pb); | |
93 c->vidpos = url_ftell(pb) + size; | |
94 url_fskip(pb, 16); | |
95 fsize = get_le32(pb); | |
96 | |
97 ast = av_new_stream(s, 0); | |
98 if (!ast) | |
99 return -1; | |
100 get_wav_header(pb, ast->codec, fsize); | |
101 // find 'data' chunk | |
102 while(url_ftell(pb) < c->vidpos && !url_feof(pb)){ | |
103 tag = get_le32(pb); | |
104 fsize = get_le32(pb); | |
105 if(tag == MKTAG('d', 'a', 't', 'a')) break; | |
106 url_fskip(pb, fsize); | |
107 } | |
108 c->bpc = (fsize + c->frames - 1) / c->frames; | |
109 if(ast->codec->block_align) | |
110 c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align; | |
111 c->bytes_left = fsize; | |
112 c->wavpos = url_ftell(pb); | |
113 url_fseek(pb, c->vidpos, SEEK_SET); | |
114 } | |
115 | |
116 /* now we are ready: build format streams */ | |
117 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
118 st->codec->codec_id = CODEC_ID_DXA; | |
119 st->codec->width = w; | |
120 st->codec->height = h; | |
121 av_reduce(&den, &num, den, num, (1UL<<31)-1); | |
122 av_set_pts_info(st, 33, num, den); | |
123 /* flags & 0x80 means that image is interlaced, | |
124 * flags & 0x40 means that image has double height | |
125 * either way set true height | |
126 */ | |
127 if(flags & 0xC0){ | |
128 st->codec->height >>= 1; | |
129 } | |
130 c->readvid = !c->has_sound; | |
131 c->vidpos = url_ftell(pb); | |
132 s->start_time = 0; | |
133 s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den; | |
134 av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames); | |
135 | |
136 return 0; | |
137 } | |
138 | |
139 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt) | |
140 { | |
141 DXAContext *c = s->priv_data; | |
142 int ret; | |
143 uint32_t size; | |
144 uint8_t buf[DXA_EXTRA_SIZE], pal[768+4]; | |
145 int pal_size = 0; | |
146 | |
147 if(!c->readvid && c->has_sound && c->bytes_left){ | |
148 c->readvid = 1; | |
149 url_fseek(&s->pb, c->wavpos, SEEK_SET); | |
150 size = FFMIN(c->bytes_left, c->bpc); | |
151 ret = av_get_packet(&s->pb, pkt, size); | |
152 pkt->stream_index = 1; | |
153 if(ret != size) | |
154 return AVERROR_IO; | |
155 c->bytes_left -= size; | |
156 c->wavpos = url_ftell(&s->pb); | |
157 return 0; | |
158 } | |
159 url_fseek(&s->pb, c->vidpos, SEEK_SET); | |
160 while(!url_feof(&s->pb) && c->frames){ | |
161 get_buffer(&s->pb, buf, 4); | |
162 switch(AV_RL32(buf)){ | |
163 case MKTAG('N', 'U', 'L', 'L'): | |
164 if(av_new_packet(pkt, 4 + pal_size) < 0) | |
165 return AVERROR_NOMEM; | |
166 pkt->stream_index = 0; | |
167 if(pal_size) memcpy(pkt->data, pal, pal_size); | |
168 memcpy(pkt->data + pal_size, buf, 4); | |
169 c->frames--; | |
170 c->vidpos = url_ftell(&s->pb); | |
171 c->readvid = 0; | |
172 return 0; | |
173 case MKTAG('C', 'M', 'A', 'P'): | |
174 pal_size = 768+4; | |
175 memcpy(pal, buf, 4); | |
176 get_buffer(&s->pb, pal + 4, 768); | |
177 break; | |
178 case MKTAG('F', 'R', 'A', 'M'): | |
179 get_buffer(&s->pb, buf + 4, DXA_EXTRA_SIZE - 4); | |
180 size = AV_RB32(buf + 5); | |
181 if(size > 0xFFFFFF){ | |
182 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size); | |
183 return -1; | |
184 } | |
185 if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0) | |
186 return AVERROR_NOMEM; | |
187 memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE); | |
188 ret = get_buffer(&s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size); | |
189 if(ret != size){ | |
190 av_free_packet(pkt); | |
191 return AVERROR_IO; | |
192 } | |
193 if(pal_size) memcpy(pkt->data, pal, pal_size); | |
194 pkt->stream_index = 0; | |
195 c->frames--; | |
196 c->vidpos = url_ftell(&s->pb); | |
197 c->readvid = 0; | |
198 return 0; | |
199 default: | |
200 av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]); | |
201 return -1; | |
202 } | |
203 } | |
204 return AVERROR(EIO); | |
205 } | |
206 | |
207 AVInputFormat dxa_demuxer = { | |
208 "dxa", | |
209 "dxa", | |
210 sizeof(DXAContext), | |
211 dxa_probe, | |
212 dxa_read_header, | |
213 dxa_read_packet, | |
214 }; |