some checks to avoid segfault
[libav.git] / libavformat / avienc.c
CommitLineData
de6d9b64
FB
1/*
2 * AVI encoder.
19720f15 3 * Copyright (c) 2000 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20#include "avi.h"
21
22/*
23 * TODO:
24 * - fill all fields if non streamed (nb_frames for example)
25 */
26
27typedef struct AVIIndex {
28 unsigned char tag[4];
29 unsigned int flags, pos, len;
30 struct AVIIndex *next;
31} AVIIndex;
32
33typedef struct {
86bec9a1
J
34 offset_t movi_list, frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
35 int audio_strm_length[MAX_STREAMS];
de6d9b64
FB
36 AVIIndex *first, *last;
37} AVIContext;
38
0570bf06 39offset_t start_tag(ByteIOContext *pb, const char *tag)
de6d9b64
FB
40{
41 put_tag(pb, tag);
42 put_le32(pb, 0);
43 return url_ftell(pb);
44}
45
46void end_tag(ByteIOContext *pb, offset_t start)
47{
48 offset_t pos;
49
50 pos = url_ftell(pb);
51 url_fseek(pb, start - 4, SEEK_SET);
0c1a9eda 52 put_le32(pb, (uint32_t)(pos - start));
de6d9b64
FB
53 url_fseek(pb, pos, SEEK_SET);
54}
55
56/* Note: when encoding, the first matching tag is used, so order is
57 important if multiple tags possible for a given codec. */
0570bf06 58const CodecTag codec_bmp_tags[] = {
95c79a24
J
59 { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
60 { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
de6d9b64 61 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
a05c8d71
MN
62
63 /* added based on MPlayer */
64 { CODEC_ID_H263I, MKTAG('i', '2', '6', '3') },
65 { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
66 { CODEC_ID_H263P, MKTAG('h', '2', '6', '3') },
67 { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') },
68
de6d9b64 69 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
b1d89f82
MR
70 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
71 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x'), .invalid_asf = 1 },
72 { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
73 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
74 { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd'), .invalid_asf = 1 },
75 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's'), .invalid_asf = 1 },
390dffc5
MN
76 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
77 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
78 { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') },
58f26ba9 79 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
a05c8d71
MN
80
81 /* added based on MPlayer */
82 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
83 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', '1') },
84 { CODEC_ID_MPEG4, MKTAG('X', 'v', 'i', 'D') },
85 { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
86 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
87 { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
88
b1d89f82 89 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
8a555875 90 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '3'), .invalid_asf = 1 },
bb3debab 91 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
a05c8d71
MN
92
93 /* added based on MPlayer */
94 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
95 { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', 'g', '3') },
96 { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', '4', '3') },
97 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
98 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '5') },
99 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
100 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '6') },
101 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
102 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '4') },
103 { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
104 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
105 { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '1') },
106 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
107 { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '0') },
108
bb3debab 109 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
a05c8d71
MN
110
111 /* added based on MPlayer */
112 { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
113 { CODEC_ID_MSMPEG4V2, MKTAG('d', 'i', 'v', '2') },
114 { CODEC_ID_MSMPEG4V2, MKTAG('m', 'p', '4', '2') },
115
9b030d9d 116 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
a05c8d71
MN
117
118 /* added based on MPlayer */
119 { CODEC_ID_MSMPEG4V1, MKTAG('D', 'I', 'V', '4') },
120 { CODEC_ID_MSMPEG4V1, MKTAG('d', 'i', 'v', '4') },
121 { CODEC_ID_MSMPEG4V1, MKTAG('m', 'p', 'g', '4') },
122
9b030d9d 123 { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
a05c8d71
MN
124
125 /* added based on MPlayer */
126 { CODEC_ID_WMV1, MKTAG('w', 'm', 'v', '1') },
127
e23d5712 128 { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
020fcc94
FB
129 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
130 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
f20dca40 131 { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') },
020fcc94 132 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
1cbc2890
FB
133 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
134 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
135 { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
bb15f7fd 136 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
f560dd82
MN
137 { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
138 { CODEC_ID_HUFFYUV, MKTAG('h', 'f', 'y', 'u') },
1e711bd6
MM
139 { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
140 { CODEC_ID_CYUV, MKTAG('c', 'y', 'u', 'v') },
e8750b00 141 { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
e8b478c1 142 { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
de6d9b64
FB
143 { 0, 0 },
144};
145
a266644f 146unsigned int codec_get_tag(const CodecTag *tags, int id)
de6d9b64
FB
147{
148 while (tags->id != 0) {
149 if (tags->id == id)
150 return tags->tag;
151 tags++;
152 }
153 return 0;
154}
155
2727c35e
PG
156static unsigned int codec_get_asf_tag(const CodecTag *tags, int id)
157{
158 while (tags->id != 0) {
159 if (!tags->invalid_asf && tags->id == id)
160 return tags->tag;
161 tags++;
162 }
163 return 0;
164}
165
a266644f 166int codec_get_id(const CodecTag *tags, unsigned int tag)
de6d9b64
FB
167{
168 while (tags->id != 0) {
169 if (tags->tag == tag)
170 return tags->id;
171 tags++;
172 }
173 return 0;
174}
175
176unsigned int codec_get_bmp_tag(int id)
177{
178 return codec_get_tag(codec_bmp_tags, id);
179}
180
181/* BITMAPINFOHEADER header */
0570bf06 182void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
de6d9b64 183{
952c69c4 184 put_le32(pb, 40 + enc->extradata_size); /* size */
de6d9b64
FB
185 put_le32(pb, enc->width);
186 put_le32(pb, enc->height);
187 put_le16(pb, 1); /* planes */
952c69c4
MN
188
189 put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
de6d9b64 190 /* compression type */
2727c35e 191 put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : codec_get_tag(tags, enc->codec_id));
de6d9b64
FB
192 put_le32(pb, enc->width * enc->height * 3);
193 put_le32(pb, 0);
194 put_le32(pb, 0);
195 put_le32(pb, 0);
196 put_le32(pb, 0);
952c69c4
MN
197
198 put_buffer(pb, enc->extradata, enc->extradata_size);
199
200 if (enc->extradata_size & 1)
201 put_byte(pb, 0);
de6d9b64
FB
202}
203
0570bf06 204static void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
86bec9a1
J
205{
206 switch(stream->codec_id) {
207 case CODEC_ID_PCM_S16LE:
208 *au_scale = *au_ssize = 2*stream->channels;
209 *au_byterate = *au_ssize * stream->sample_rate;
210 break;
211 case CODEC_ID_PCM_U8:
212 case CODEC_ID_PCM_ALAW:
213 case CODEC_ID_PCM_MULAW:
214 *au_scale = *au_ssize = stream->channels;
215 *au_byterate = *au_ssize * stream->sample_rate;
216 break;
217 case CODEC_ID_MP2:
218 *au_ssize = 1;
219 *au_scale = 1;
220 *au_byterate = stream->bit_rate / 8;
5798368b
J
221 case CODEC_ID_MP3LAME:
222 *au_ssize = 1;
223 *au_scale = 1;
224 *au_byterate = stream->bit_rate / 8;
86bec9a1
J
225 default:
226 *au_ssize = 1;
227 *au_scale = 1;
228 *au_byterate = stream->bit_rate / 8;
229 break;
230 }
231}
232
de6d9b64
FB
233static int avi_write_header(AVFormatContext *s)
234{
c9a65ca8 235 AVIContext *avi = s->priv_data;
de6d9b64 236 ByteIOContext *pb = &s->pb;
86bec9a1 237 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
de6d9b64
FB
238 AVCodecContext *stream, *video_enc;
239 offset_t list1, list2, strh, strf;
240
de6d9b64
FB
241 put_tag(pb, "RIFF");
242 put_le32(pb, 0); /* file length */
243 put_tag(pb, "AVI ");
244
245 /* header list */
246 list1 = start_tag(pb, "LIST");
247 put_tag(pb, "hdrl");
248
249 /* avi header */
250 put_tag(pb, "avih");
251 put_le32(pb, 14 * 4);
252 bitrate = 0;
253
254 video_enc = NULL;
255 for(n=0;n<s->nb_streams;n++) {
256 stream = &s->streams[n]->codec;
257 bitrate += stream->bit_rate;
258 if (stream->codec_type == CODEC_TYPE_VIDEO)
259 video_enc = stream;
260 }
261
850742d7
AR
262/* allowing audio-only AVI file
263
de6d9b64 264 if (!video_enc) {
1ea4f593 265 av_free(avi);
de6d9b64
FB
266 return -1;
267 }
850742d7 268*/
de6d9b64
FB
269 nb_frames = 0;
270
850742d7 271 if(video_enc){
14bea432 272 put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->frame_rate_base / video_enc->frame_rate));
850742d7
AR
273 } else {
274 put_le32(pb, 0);
275 }
de6d9b64
FB
276 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
277 put_le32(pb, 0); /* padding */
278 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
86bec9a1 279 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
280 put_le32(pb, nb_frames); /* nb frames, filled later */
281 put_le32(pb, 0); /* initial frame */
282 put_le32(pb, s->nb_streams); /* nb streams */
283 put_le32(pb, 1024 * 1024); /* suggested buffer size */
850742d7 284 if(video_enc){
de6d9b64
FB
285 put_le32(pb, video_enc->width);
286 put_le32(pb, video_enc->height);
850742d7
AR
287 } else {
288 put_le32(pb, 0);
289 put_le32(pb, 0);
290 }
de6d9b64
FB
291 put_le32(pb, 0); /* reserved */
292 put_le32(pb, 0); /* reserved */
293 put_le32(pb, 0); /* reserved */
294 put_le32(pb, 0); /* reserved */
295
296 /* stream list */
297 for(i=0;i<n;i++) {
298 list2 = start_tag(pb, "LIST");
299 put_tag(pb, "strl");
300
301 stream = &s->streams[i]->codec;
302
e8750b00
FR
303 /* FourCC should really be set by the codec itself */
304 if (! stream->codec_tag) {
305 stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
306 }
307
de6d9b64
FB
308 /* stream generic header */
309 strh = start_tag(pb, "strh");
310 switch(stream->codec_type) {
311 case CODEC_TYPE_VIDEO:
312 put_tag(pb, "vids");
e8750b00 313 put_le32(pb, stream->codec_tag);
de6d9b64
FB
314 put_le32(pb, 0); /* flags */
315 put_le16(pb, 0); /* priority */
316 put_le16(pb, 0); /* language */
317 put_le32(pb, 0); /* initial frame */
b559b29b 318
14bea432
MN
319 put_le32(pb, stream->frame_rate_base); /* scale */
320 put_le32(pb, stream->frame_rate); /* rate */
b559b29b 321
de6d9b64 322 put_le32(pb, 0); /* start */
86bec9a1 323 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
324 put_le32(pb, nb_frames); /* length, XXX: fill later */
325 put_le32(pb, 1024 * 1024); /* suggested buffer size */
86bec9a1 326 put_le32(pb, -1); /* quality */
de6d9b64
FB
327 put_le32(pb, stream->width * stream->height * 3); /* sample size */
328 put_le16(pb, 0);
329 put_le16(pb, 0);
330 put_le16(pb, stream->width);
331 put_le16(pb, stream->height);
332 break;
333 case CODEC_TYPE_AUDIO:
334 put_tag(pb, "auds");
86bec9a1 335 put_le32(pb, 1); /* tag */
de6d9b64
FB
336 put_le32(pb, 0); /* flags */
337 put_le16(pb, 0); /* priority */
338 put_le16(pb, 0); /* language */
339 put_le32(pb, 0); /* initial frame */
86bec9a1
J
340 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
341 put_le32(pb, au_scale); /* scale */
342 put_le32(pb, au_byterate); /* rate */
de6d9b64 343 put_le32(pb, 0); /* start */
86bec9a1 344 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
345 put_le32(pb, 0); /* length, XXX: filled later */
346 put_le32(pb, 12 * 1024); /* suggested buffer size */
347 put_le32(pb, -1); /* quality */
86bec9a1 348 put_le32(pb, au_ssize); /* sample size */
de6d9b64
FB
349 put_le32(pb, 0);
350 put_le32(pb, 0);
351 break;
53cafac0 352 default:
42343f7e 353 av_abort();
de6d9b64
FB
354 }
355 end_tag(pb, strh);
356
357 strf = start_tag(pb, "strf");
358 switch(stream->codec_type) {
359 case CODEC_TYPE_VIDEO:
2727c35e 360 put_bmp_header(pb, stream, codec_bmp_tags, 0);
de6d9b64
FB
361 break;
362 case CODEC_TYPE_AUDIO:
46a3d068 363 if (put_wav_header(pb, stream) < 0) {
1ea4f593 364 av_free(avi);
46a3d068
FB
365 return -1;
366 }
de6d9b64 367 break;
53cafac0 368 default:
42343f7e 369 av_abort();
de6d9b64
FB
370 }
371 end_tag(pb, strf);
372 end_tag(pb, list2);
373 }
374
375 end_tag(pb, list1);
376
377 avi->movi_list = start_tag(pb, "LIST");
378 avi->first = NULL;
379 avi->last = NULL;
380 put_tag(pb, "movi");
381
382 put_flush_packet(pb);
383
384 return 0;
385}
386
387static int avi_write_packet(AVFormatContext *s, int stream_index,
0c1a9eda 388 uint8_t *buf, int size, int force_pts)
de6d9b64
FB
389{
390 AVIContext *avi = s->priv_data;
391 ByteIOContext *pb = &s->pb;
392 AVIIndex *idx;
393 unsigned char tag[5];
394 unsigned int flags;
395 AVCodecContext *enc;
396
397 enc = &s->streams[stream_index]->codec;
398
399 tag[0] = '0';
400 tag[1] = '0' + stream_index;
401 if (enc->codec_type == CODEC_TYPE_VIDEO) {
402 tag[2] = 'd';
403 tag[3] = 'c';
492cd3a9 404 flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
de6d9b64
FB
405 } else {
406 tag[2] = 'w';
407 tag[3] = 'b';
408 flags = 0x10;
409 }
86bec9a1
J
410 if (enc->codec_type == CODEC_TYPE_AUDIO)
411 avi->audio_strm_length[stream_index] += size;
de6d9b64
FB
412
413 if (!url_is_streamed(&s->pb)) {
1ea4f593 414 idx = av_malloc(sizeof(AVIIndex));
de6d9b64
FB
415 memcpy(idx->tag, tag, 4);
416 idx->flags = flags;
417 idx->pos = url_ftell(pb) - avi->movi_list;
418 idx->len = size;
419 idx->next = NULL;
420 if (!avi->last)
421 avi->first = idx;
422 else
423 avi->last->next = idx;
424 avi->last = idx;
425 }
426
427 put_buffer(pb, tag, 4);
428 put_le32(pb, size);
429 put_buffer(pb, buf, size);
430 if (size & 1)
431 put_byte(pb, 0);
432
433 put_flush_packet(pb);
434 return 0;
435}
436
437static int avi_write_trailer(AVFormatContext *s)
438{
439 ByteIOContext *pb = &s->pb;
440 AVIContext *avi = s->priv_data;
441 offset_t file_size, idx_chunk;
86bec9a1
J
442 int n, nb_frames, au_byterate, au_ssize, au_scale;
443 AVCodecContext *stream;
de6d9b64
FB
444 AVIIndex *idx;
445
446 if (!url_is_streamed(&s->pb)) {
447 end_tag(pb, avi->movi_list);
448
449 idx_chunk = start_tag(pb, "idx1");
450 idx = avi->first;
451 while (idx != NULL) {
452 put_buffer(pb, idx->tag, 4);
453 put_le32(pb, idx->flags);
454 put_le32(pb, idx->pos);
455 put_le32(pb, idx->len);
456 idx = idx->next;
457 }
458 end_tag(pb, idx_chunk);
459
460 /* update file size */
461 file_size = url_ftell(pb);
462 url_fseek(pb, 4, SEEK_SET);
0c1a9eda 463 put_le32(pb, (uint32_t)(file_size - 8));
86bec9a1
J
464
465 /* Fill in frame/sample counters */
466 nb_frames = 0;
467 for(n=0;n<s->nb_streams;n++) {
468 if (avi->frames_hdr_strm[n] != 0) {
469 stream = &s->streams[n]->codec;
470 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
471 if (stream->codec_type == CODEC_TYPE_VIDEO) {
472 put_le32(pb, stream->frame_number);
473 if (nb_frames < stream->frame_number)
474 nb_frames = stream->frame_number;
475 } else {
5798368b 476 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
86bec9a1
J
477 put_le32(pb, stream->frame_number);
478 nb_frames += stream->frame_number;
479 } else {
480 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
481 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
482 }
483 }
484 }
485 }
486 if (avi->frames_hdr_all != 0) {
487 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
488 put_le32(pb, nb_frames);
489 }
de6d9b64
FB
490 url_fseek(pb, file_size, SEEK_SET);
491 }
492 put_flush_packet(pb);
de6d9b64
FB
493 return 0;
494}
495
c9a65ca8 496static AVOutputFormat avi_oformat = {
de6d9b64
FB
497 "avi",
498 "avi format",
499 "video/x-msvideo",
500 "avi",
c9a65ca8 501 sizeof(AVIContext),
de6d9b64 502 CODEC_ID_MP2,
d7425f59 503 CODEC_ID_MSMPEG4V3,
de6d9b64
FB
504 avi_write_header,
505 avi_write_packet,
506 avi_write_trailer,
de6d9b64 507};
c9a65ca8
FB
508
509int avienc_init(void)
510{
511 av_register_output_format(&avi_oformat);
512 return 0;
513}