h264/aarch64: optimize neon loop filter
[libav.git] / libavformat / sierravmd.c
1 /*
2 * Sierra VMD Format Demuxer
3 * Copyright (c) 2004 The FFmpeg project
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 * Sierra VMD file demuxer
25 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26 * for more information on the Sierra VMD file format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 */
29
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
34
35 #define VMD_HEADER_SIZE 0x0330
36 #define BYTES_PER_FRAME_RECORD 16
37
38 typedef struct vmd_frame {
39 int stream_index;
40 int64_t frame_offset;
41 unsigned int frame_size;
42 int64_t pts;
43 int keyframe;
44 unsigned char frame_record[BYTES_PER_FRAME_RECORD];
45 } vmd_frame;
46
47 typedef struct VmdDemuxContext {
48 int video_stream_index;
49 int audio_stream_index;
50
51 unsigned int frame_count;
52 unsigned int frames_per_block;
53 vmd_frame *frame_table;
54 unsigned int current_frame;
55 int is_indeo3;
56
57 int sample_rate;
58 int64_t audio_sample_counter;
59 int skiphdr;
60
61 unsigned char vmd_header[VMD_HEADER_SIZE];
62 } VmdDemuxContext;
63
64 static int vmd_probe(AVProbeData *p)
65 {
66 int w, h;
67 if (p->buf_size < 16)
68 return 0;
69 /* check if the first 2 bytes of the file contain the appropriate size
70 * of a VMD header chunk */
71 if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
72 return 0;
73 w = AV_RL16(&p->buf[12]);
74 h = AV_RL16(&p->buf[14]);
75 if (!w || w > 2048 || !h || h > 2048)
76 return 0;
77
78 /* only return half certainty since this check is a bit sketchy */
79 return AVPROBE_SCORE_EXTENSION;
80 }
81
82 static int vmd_read_header(AVFormatContext *s)
83 {
84 VmdDemuxContext *vmd = s->priv_data;
85 AVIOContext *pb = s->pb;
86 AVStream *st = NULL, *vst;
87 unsigned int toc_offset;
88 unsigned char *raw_frame_table;
89 int raw_frame_table_size;
90 int64_t current_offset;
91 int i, j, ret;
92 unsigned int total_frames;
93 int64_t current_audio_pts = 0;
94 unsigned char chunk[BYTES_PER_FRAME_RECORD];
95 int num, den;
96 int sound_buffers;
97
98 /* fetch the main header, including the 2 header length bytes */
99 avio_seek(pb, 0, SEEK_SET);
100 if (avio_read(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
101 return AVERROR(EIO);
102
103 if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3')
104 vmd->is_indeo3 = 1;
105 else
106 vmd->is_indeo3 = 0;
107 /* start up the decoders */
108 vst = avformat_new_stream(s, NULL);
109 if (!vst)
110 return AVERROR(ENOMEM);
111 avpriv_set_pts_info(vst, 33, 1, 10);
112 vmd->video_stream_index = vst->index;
113 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
114 vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
115 vst->codecpar->codec_tag = 0; /* no fourcc */
116 vst->codecpar->width = AV_RL16(&vmd->vmd_header[12]);
117 vst->codecpar->height = AV_RL16(&vmd->vmd_header[14]);
118 if(vmd->is_indeo3 && vst->codecpar->width > 320){
119 vst->codecpar->width >>= 1;
120 vst->codecpar->height >>= 1;
121 }
122 vst->codecpar->extradata_size = VMD_HEADER_SIZE;
123 vst->codecpar->extradata = av_mallocz(VMD_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
124 memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
125
126 /* if sample rate is 0, assume no audio */
127 vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
128 if (vmd->sample_rate) {
129 st = avformat_new_stream(s, NULL);
130 if (!st)
131 return AVERROR(ENOMEM);
132 vmd->audio_stream_index = st->index;
133 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
134 st->codecpar->codec_id = AV_CODEC_ID_VMDAUDIO;
135 st->codecpar->codec_tag = 0; /* no fourcc */
136 if (vmd->vmd_header[811] & 0x80) {
137 st->codecpar->channels = 2;
138 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
139 } else {
140 st->codecpar->channels = 1;
141 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
142 }
143 st->codecpar->sample_rate = vmd->sample_rate;
144 st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
145 if (st->codecpar->block_align & 0x8000) {
146 st->codecpar->bits_per_coded_sample = 16;
147 st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
148 } else {
149 st->codecpar->bits_per_coded_sample = 8;
150 }
151 st->codecpar->bit_rate = st->codecpar->sample_rate *
152 st->codecpar->bits_per_coded_sample * st->codecpar->channels;
153
154 /* calculate pts */
155 num = st->codecpar->block_align;
156 den = st->codecpar->sample_rate * st->codecpar->channels;
157 av_reduce(&den, &num, den, num, (1UL<<31)-1);
158 avpriv_set_pts_info(vst, 33, num, den);
159 avpriv_set_pts_info(st, 33, num, den);
160 }
161
162 toc_offset = AV_RL32(&vmd->vmd_header[812]);
163 vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
164 vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
165 avio_seek(pb, toc_offset, SEEK_SET);
166
167 raw_frame_table = NULL;
168 vmd->frame_table = NULL;
169 sound_buffers = AV_RL16(&vmd->vmd_header[808]);
170 raw_frame_table_size = vmd->frame_count * 6;
171 if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
172 av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
173 return -1;
174 }
175 raw_frame_table = av_malloc(raw_frame_table_size);
176 vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
177 if (!raw_frame_table || !vmd->frame_table) {
178 ret = AVERROR(ENOMEM);
179 goto error;
180 }
181 if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
182 raw_frame_table_size) {
183 ret = AVERROR(EIO);
184 goto error;
185 }
186
187 total_frames = 0;
188 for (i = 0; i < vmd->frame_count; i++) {
189
190 current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
191
192 /* handle each entry in index block */
193 for (j = 0; j < vmd->frames_per_block; j++) {
194 int type;
195 uint32_t size;
196
197 avio_read(pb, chunk, BYTES_PER_FRAME_RECORD);
198 type = chunk[0];
199 size = AV_RL32(&chunk[2]);
200 if (size > INT_MAX / 2) {
201 av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
202 ret = AVERROR_INVALIDDATA;
203 goto error;
204 }
205 if(!size && type != 1)
206 continue;
207 switch(type) {
208 case 1: /* Audio Chunk */
209 if (!st) break;
210 /* first audio chunk contains several audio buffers */
211 vmd->frame_table[total_frames].frame_offset = current_offset;
212 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
213 vmd->frame_table[total_frames].frame_size = size;
214 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
215 vmd->frame_table[total_frames].pts = current_audio_pts;
216 total_frames++;
217 if(!current_audio_pts)
218 current_audio_pts += sound_buffers - 1;
219 else
220 current_audio_pts++;
221 break;
222 case 2: /* Video Chunk */
223 vmd->frame_table[total_frames].frame_offset = current_offset;
224 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
225 vmd->frame_table[total_frames].frame_size = size;
226 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
227 vmd->frame_table[total_frames].pts = i;
228 total_frames++;
229 break;
230 }
231 current_offset += size;
232 }
233 }
234
235 av_free(raw_frame_table);
236
237 vmd->current_frame = 0;
238 vmd->frame_count = total_frames;
239
240 return 0;
241
242 error:
243 av_free(raw_frame_table);
244 av_free(vmd->frame_table);
245 return ret;
246 }
247
248 static int vmd_read_packet(AVFormatContext *s,
249 AVPacket *pkt)
250 {
251 VmdDemuxContext *vmd = s->priv_data;
252 AVIOContext *pb = s->pb;
253 int ret = 0;
254 vmd_frame *frame;
255
256 if (vmd->current_frame >= vmd->frame_count)
257 return AVERROR(EIO);
258
259 frame = &vmd->frame_table[vmd->current_frame];
260 /* position the stream (will probably be there already) */
261 avio_seek(pb, frame->frame_offset, SEEK_SET);
262
263 if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
264 return AVERROR(ENOMEM);
265 pkt->pos= avio_tell(pb);
266 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
267 if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
268 ret = avio_read(pb, pkt->data, frame->frame_size);
269 else
270 ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
271 frame->frame_size);
272
273 if (ret != frame->frame_size) {
274 av_packet_unref(pkt);
275 ret = AVERROR(EIO);
276 }
277 pkt->stream_index = frame->stream_index;
278 pkt->pts = frame->pts;
279 av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
280 (frame->frame_record[0] == 0x02) ? "video" : "audio",
281 frame->frame_size + BYTES_PER_FRAME_RECORD,
282 pkt->pts);
283
284 vmd->current_frame++;
285
286 return ret;
287 }
288
289 static int vmd_read_close(AVFormatContext *s)
290 {
291 VmdDemuxContext *vmd = s->priv_data;
292
293 av_free(vmd->frame_table);
294
295 return 0;
296 }
297
298 AVInputFormat ff_vmd_demuxer = {
299 .name = "vmd",
300 .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
301 .priv_data_size = sizeof(VmdDemuxContext),
302 .read_probe = vmd_probe,
303 .read_header = vmd_read_header,
304 .read_packet = vmd_read_packet,
305 .read_close = vmd_read_close,
306 };