Mercurial > libavformat.hg
comparison nut.c @ 219:2f16e3066399 libavformat
initial nut muxer and demuxer (demuxer is not fail safe)
author | al3x |
---|---|
date | Fri, 05 Sep 2003 18:45:32 +0000 |
parents | |
children | 1dbacadcd222 |
comparison
equal
deleted
inserted
replaced
218:7fc8e8ee081a | 219:2f16e3066399 |
---|---|
1 /* | |
2 * NUT (de)muxer based on initial draft | |
3 * Copyright (c) 2003 Alex Beregszaszi | |
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 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 * NUT DRAFT can be found in MPlayer CVS at DOCS/tech/mpcf.txt | |
20 * | |
21 * Compatible with draft version 20030906 | |
22 * | |
23 */ | |
24 | |
25 /* | |
26 * TODO: | |
27 * - checksumming | |
28 * - correct rate denom/nom and sample_mul | |
29 * - correct timestamp handling | |
30 * - correct startcodes | |
31 * - index writing | |
32 * - info and index packet reading support | |
33 * - startcode searching for broken streams | |
34 * - subpacket support | |
35 * - handling of codec specific headers | |
36 */ | |
37 | |
38 //#define DEBUG 1 | |
39 | |
40 #include "avformat.h" | |
41 #include "mpegaudio.h" | |
42 | |
43 typedef struct { | |
44 int curr_frame_start; | |
45 int last_frame_size; | |
46 int curr_frame_size; | |
47 } NUTContext; | |
48 | |
49 static int bytes_left(ByteIOContext *bc) | |
50 { | |
51 return bc->buf_end - bc->buf_ptr; | |
52 } | |
53 | |
54 static uint64_t get_v(ByteIOContext *bc) | |
55 { | |
56 uint64_t val = 0; | |
57 | |
58 // for (; bytes_left(s)*8 > 0; ) | |
59 for(; bytes_left(bc) > 0; ) | |
60 { | |
61 int tmp = get_byte(bc); | |
62 | |
63 if (tmp&0x80) | |
64 val= (val<<7) + tmp - 0x80; | |
65 else | |
66 return (val<<7) + tmp; | |
67 } | |
68 return -1; | |
69 } | |
70 | |
71 static int64_t get_s(ByteIOContext *bc) | |
72 { | |
73 int64_t v = get_v(bc) + 1; | |
74 | |
75 if (v&1) | |
76 return -(v>>1); | |
77 else | |
78 return (v>>1); | |
79 } | |
80 | |
81 static int get_b(ByteIOContext *bc, char *data, int maxlen) | |
82 { | |
83 int i, len; | |
84 | |
85 len = get_v(bc); | |
86 for (i = 0; i < len && i < maxlen; i++) | |
87 data[i] = get_byte(bc); | |
88 if (i < len) | |
89 { | |
90 len = i; | |
91 for (i = 0; i < len; i++) | |
92 get_byte(bc); | |
93 } | |
94 | |
95 return 0; | |
96 } | |
97 | |
98 static int get_packetheader(NUTContext *nut, ByteIOContext *bc) | |
99 { | |
100 nut->curr_frame_start = url_ftell(bc); | |
101 nut->curr_frame_size = get_v(bc); | |
102 nut->last_frame_size = get_v(bc); | |
103 dprintf("Packet: fwd: %d bwd: %d\n", | |
104 nut->curr_frame_size, nut->last_frame_size); | |
105 | |
106 return 0; | |
107 } | |
108 | |
109 static int get_padding(NUTContext *nut, ByteIOContext *bc) | |
110 { | |
111 int i, tmp, len = nut->curr_frame_size - (url_ftell(bc) - nut->curr_frame_start); | |
112 | |
113 for (i = 0; i < len; i++) | |
114 { | |
115 tmp = get_byte(bc); | |
116 if (tmp != 0) | |
117 fprintf(stderr, "bad padding\n"); | |
118 } | |
119 | |
120 return 0; | |
121 } | |
122 | |
123 static int put_v(ByteIOContext *bc, uint64_t val) | |
124 { | |
125 int i; | |
126 | |
127 // if (bytes_left(s)*8 < 9) | |
128 // return -1; | |
129 | |
130 if (bytes_left(bc) < 1) | |
131 return -1; | |
132 | |
133 val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently | |
134 for (i=7; ; i+=7) | |
135 if ((val>>i) == 0) | |
136 break; | |
137 | |
138 for (i-=7; i>0; i-=8) | |
139 put_byte(bc, 0x80 | (val>>i)); | |
140 | |
141 put_byte(bc, val&0x7f); | |
142 | |
143 return 0; | |
144 } | |
145 | |
146 static int put_s(ByteIOContext *bc, uint64_t val) | |
147 { | |
148 if (val<=0) | |
149 return put_v(bc, -2*val); | |
150 else | |
151 return put_v(bc, 2*val-1); | |
152 } | |
153 | |
154 static int put_b(ByteIOContext *bc, char *data, int len) | |
155 { | |
156 int i; | |
157 | |
158 put_v(bc, len); | |
159 for (i = 0; i < len; i++) | |
160 put_byte(bc, data[i]); | |
161 | |
162 return 0; | |
163 } | |
164 | |
165 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int est_size) | |
166 { | |
167 put_flush_packet(bc); | |
168 nut->curr_frame_start = url_ftell(bc); | |
169 nut->curr_frame_size = est_size; | |
170 | |
171 /* packet header */ | |
172 put_v(bc, nut->curr_frame_size); /* forward ptr */ | |
173 put_v(bc, nut->last_frame_size); /* backward ptr */ | |
174 dprintf("Packet: fwd: %d, bwd: %d\n", | |
175 nut->curr_frame_size, nut->last_frame_size); | |
176 | |
177 nut->last_frame_size = nut->curr_frame_size; | |
178 | |
179 return 0; | |
180 } | |
181 | |
182 static int put_padding(NUTContext *nut, ByteIOContext *bc) | |
183 { | |
184 int i, len = nut->curr_frame_size - (url_ftell(bc) - nut->curr_frame_start); | |
185 | |
186 put_flush_packet(bc); | |
187 for (i = 0; i < len; i++) | |
188 put_byte(bc, 0); | |
189 | |
190 dprintf("padded %d bytes\n", i); | |
191 | |
192 return 0; | |
193 } | |
194 | |
195 static int nut_write_header(AVFormatContext *s) | |
196 { | |
197 NUTContext *nut = s->priv_data; | |
198 ByteIOContext *bc = &s->pb; | |
199 AVCodecContext *codec; | |
200 int i; | |
201 int stream_length = 0; | |
202 | |
203 for (i = 0; i < s->nb_streams; i++) | |
204 { | |
205 if (stream_length < (s->streams[i]->duration * (AV_TIME_BASE / 1000))) | |
206 stream_length = s->streams[i]->duration * (AV_TIME_BASE / 1000); | |
207 } | |
208 | |
209 put_packetheader(nut, bc, 16); /* FIXME: estimation */ | |
210 | |
211 /* main header */ | |
212 put_le64(bc, 1); /* FIXME: unique startcode */ | |
213 put_v(bc, 0); /* version */ | |
214 put_v(bc, s->nb_streams); | |
215 put_v(bc, 0); /* file size */ | |
216 put_v(bc, stream_length); /* len in msec */ | |
217 put_padding(nut, bc); | |
218 put_le32(bc, 0); /* FIXME: checksum */ | |
219 | |
220 /* stream headers */ | |
221 for (i = 0; i < s->nb_streams; i++) | |
222 { | |
223 codec = &s->streams[i]->codec; | |
224 | |
225 put_packetheader(nut, bc, 64); /* FIXME: estimation */ | |
226 put_le64(bc, 1); /* FIXME: unique startcode */ | |
227 put_v(bc, s->streams[i]->index); | |
228 put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0); | |
229 if (codec->codec_tag) | |
230 put_b(bc, codec->codec_tag, 4); | |
231 else if (codec->codec_type == CODEC_TYPE_VIDEO) | |
232 { | |
233 int tmp = codec_get_bmp_tag(codec->codec_id); | |
234 put_b(bc, &tmp, 4); | |
235 // put_v(bc, 4); /* len */ | |
236 // put_le32(bc, codec_get_bmp_tag(codec->codec_id)); | |
237 } | |
238 else if (codec->codec_type == CODEC_TYPE_AUDIO) | |
239 { | |
240 int tmp = codec_get_wav_tag(codec->codec_id); | |
241 put_b(bc, &tmp, 4); | |
242 // put_v(bc, 4); /* len */ | |
243 // put_le32(bc, codec_get_wav_tag(codec->codec_id)); | |
244 } | |
245 put_v(bc, codec->bit_rate); | |
246 put_v(bc, 0); /* no language code */ | |
247 put_v(bc, codec->frame_rate_base); | |
248 put_v(bc, codec->frame_rate); | |
249 put_v(bc, 0); /* timestamp_shift */ | |
250 put_v(bc, 0); /* shuffle type */ | |
251 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */ | |
252 | |
253 put_v(bc, 0); /* no codec specific headers */ | |
254 | |
255 switch(codec->codec_type) | |
256 { | |
257 case CODEC_TYPE_AUDIO: | |
258 put_v(bc, codec->sample_rate / (double)(codec->frame_rate_base / codec->frame_rate)); | |
259 put_v(bc, codec->channels); | |
260 put_padding(nut, bc); | |
261 put_le32(bc, 0); /* FIXME: checksum */ | |
262 break; | |
263 case CODEC_TYPE_VIDEO: | |
264 put_v(bc, codec->width); | |
265 put_v(bc, codec->height); | |
266 put_v(bc, 0); /* aspected w */ | |
267 put_v(bc, 0); /* aspected h */ | |
268 put_v(bc, 0); /* csp type -- unknown */ | |
269 put_padding(nut, bc); | |
270 put_le32(bc, 0); /* FIXME: checksum */ | |
271 break; | |
272 } | |
273 } | |
274 | |
275 #if 0 | |
276 /* info header */ | |
277 put_packetheader(nut, bc, 16+strlen(s->author)+strlen(s->title)+ | |
278 strlen(s->comment)+strlen(s->copyright)); /* FIXME: estimation */ | |
279 put_le64(bc, 1); /* FIXME: unique startcode */ | |
280 if (s->author[0]) | |
281 { | |
282 put_v(bc, 5); /* type */ | |
283 put_b(bc, s->author, strlen(s->author)); | |
284 } | |
285 if (s->title[0]) | |
286 { | |
287 put_v(bc, 6); /* type */ | |
288 put_b(bc, s->title, strlen(s->title)); | |
289 } | |
290 if (s->comment[0]) | |
291 { | |
292 put_v(bc, 7); /* type */ | |
293 put_b(bc, s->comment, strlen(s->comment)); | |
294 } | |
295 if (s->copyright[0]) | |
296 { | |
297 put_v(bc, 8); /* type */ | |
298 put_b(bc, s->copyright, strlen(s->copyright)); | |
299 } | |
300 /* encoder */ | |
301 put_v(bc, 9); /* type */ | |
302 put_b(bc, LIBAVFORMAT_IDENT, strlen(LIBAVFORMAT_IDENT)); | |
303 | |
304 put_padding(nut, bc); | |
305 put_le32(bc, 0); /* FIXME: checksum */ | |
306 #endif | |
307 | |
308 put_flush_packet(bc); | |
309 | |
310 return 0; | |
311 } | |
312 | |
313 static int nut_write_packet(AVFormatContext *s, int stream_index, | |
314 uint8_t *buf, int size, int force_pts) | |
315 { | |
316 NUTContext *nut = s->priv_data; | |
317 ByteIOContext *bc = &s->pb; | |
318 int key_frame = 0; | |
319 AVCodecContext *enc; | |
320 | |
321 if (stream_index > s->nb_streams) | |
322 return 1; | |
323 | |
324 enc = &s->streams[stream_index]->codec; | |
325 if (enc->codec_type == CODEC_TYPE_VIDEO) | |
326 key_frame = enc->coded_frame->key_frame; | |
327 | |
328 put_packetheader(nut, bc, size+(key_frame?16:8)+4); /* FIXME: estimation */ | |
329 | |
330 if (key_frame) | |
331 put_le64(bc, 1); /* FIXME: unique startcode */ | |
332 put_byte(bc, (key_frame ? 1<<5 : 0) + (1 << 1)); /* flags */ | |
333 put_v(bc, stream_index); | |
334 put_s(bc, 0); /* lsb_timestamp */ | |
335 | |
336 put_buffer(bc, buf, size); | |
337 | |
338 put_padding(nut, bc); | |
339 | |
340 put_flush_packet(bc); | |
341 | |
342 return 0; | |
343 } | |
344 | |
345 static int nut_write_trailer(AVFormatContext *s) | |
346 { | |
347 NUTContext *nut = s->priv_data; | |
348 ByteIOContext *bc = &s->pb; | |
349 #if 0 | |
350 int i; | |
351 | |
352 /* WRITE INDEX */ | |
353 | |
354 for (i = 0; s->nb_streams; i++) | |
355 { | |
356 put_packetheader(nut, bc, 64); /* FIXME: estimation */ | |
357 put_le64(bc, 1); /* FIXME: unique startcode */ | |
358 put_v(bc, s->streams[i]->id); | |
359 put_v(bc, ...); | |
360 put_padding(nut, bc); | |
361 put_le32(bc, 0); /* FIXME: checksum */ | |
362 } | |
363 #endif | |
364 | |
365 put_flush_packet(bc); | |
366 | |
367 return 0; | |
368 } | |
369 | |
370 static int nut_probe(AVProbeData *p) | |
371 { | |
372 return AVPROBE_SCORE_MAX; | |
373 } | |
374 | |
375 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
376 { | |
377 NUTContext *nut = s->priv_data; | |
378 ByteIOContext *bc = &s->pb; | |
379 int tmp; | |
380 int cur_stream, nb_streams; | |
381 | |
382 /* main header */ | |
383 get_packetheader(nut, bc); | |
384 tmp = get_le64(bc); | |
385 if (tmp != 1) | |
386 fprintf(stderr, "damaged? startcode!=1 (%d)\n", tmp); | |
387 | |
388 tmp = get_v(bc); | |
389 if (tmp != 0) | |
390 fprintf(stderr, "bad version (%d)\n", tmp); | |
391 | |
392 nb_streams = get_v(bc); | |
393 | |
394 s->file_size = get_v(bc); | |
395 s->duration = get_v(bc) / (AV_TIME_BASE / 1000); | |
396 | |
397 get_padding(nut, bc); | |
398 get_le32(bc); /* checkusm */ | |
399 | |
400 s->bit_rate = 0; | |
401 | |
402 /* stream header */ | |
403 for (cur_stream = 0; cur_stream < nb_streams; cur_stream++) | |
404 { | |
405 int class; | |
406 AVStream *st; | |
407 | |
408 get_packetheader(nut, bc); | |
409 tmp = get_le64(bc); | |
410 if (tmp != 1) | |
411 fprintf(stderr, "damaged? startcode!=1 (%d)\n", tmp); | |
412 st = av_new_stream(s, get_v(bc)); | |
413 if (!st) | |
414 return AVERROR_NOMEM; | |
415 class = get_v(bc); | |
416 switch(class) | |
417 { | |
418 case 0: | |
419 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
420 // get_v(bc); | |
421 // tmp = get_le32(bc); | |
422 get_b(bc, &tmp, 4); | |
423 st->codec.codec_id = codec_get_bmp_id(tmp); | |
424 if (st->codec.codec_id == CODEC_ID_NONE) | |
425 fprintf(stderr, "Unknown codec?!\n"); | |
426 break; | |
427 case 32: | |
428 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
429 // tmp = get_v(bc); | |
430 // tmp = get_le32(bc); | |
431 get_b(bc, &tmp, 4); | |
432 st->codec.codec_id = codec_get_wav_id(tmp); | |
433 if (st->codec.codec_id == CODEC_ID_NONE) | |
434 fprintf(stderr, "Unknown codec?!\n"); | |
435 break; | |
436 default: | |
437 fprintf(stderr, "Unknown stream class (%d)\n", class); | |
438 return -1; | |
439 } | |
440 s->bit_rate += get_v(bc); | |
441 tmp = get_v(bc); /* language code */ | |
442 while(tmp--) | |
443 get_byte(bc); | |
444 st->codec.frame_rate_base = get_v(bc); | |
445 st->codec.frame_rate = get_v(bc); | |
446 get_v(bc); /* FIXME: msb timestamp base */ | |
447 get_v(bc); /* shuffle type */ | |
448 get_byte(bc); /* flags */ | |
449 | |
450 get_v(bc); /* FIXME: codec specific data headers */ | |
451 | |
452 if (class == 0) /* VIDEO */ | |
453 { | |
454 st->codec.width = get_v(bc); | |
455 st->codec.height = get_v(bc); | |
456 get_v(bc); /* aspected w */ | |
457 get_v(bc); /* aspected h */ | |
458 get_v(bc); /* csp type */ | |
459 get_padding(nut, bc); | |
460 get_le32(bc); /* checksum */ | |
461 } | |
462 if (class == 32) /* AUDIO */ | |
463 { | |
464 st->codec.sample_rate = get_v(bc) * (double)(st->codec.frame_rate_base / st->codec.frame_rate); | |
465 st->codec.channels = get_v(bc); | |
466 get_padding(nut, bc); | |
467 get_le32(bc); /* checksum */ | |
468 } | |
469 } | |
470 | |
471 return 0; | |
472 } | |
473 | |
474 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt) | |
475 { | |
476 NUTContext *nut = s->priv_data; | |
477 ByteIOContext *bc = &s->pb; | |
478 int tmp, id, timestamp, size; | |
479 int key_frame = 0; | |
480 | |
481 get_packetheader(nut, bc); | |
482 | |
483 if (url_feof(bc)) | |
484 return -1; | |
485 | |
486 tmp = get_byte(bc); | |
487 if ((tmp & 0x7f) == 1) /* zero bit set? */ | |
488 { | |
489 tmp = get_le32(bc)+get_le16(bc)+get_byte(bc); | |
490 if (!tmp) | |
491 { | |
492 key_frame = 1; | |
493 tmp = get_byte(bc); /* flags */ | |
494 } | |
495 else | |
496 fprintf(stderr, "error in zero bit / startcode\n"); | |
497 } | |
498 if ((tmp & 0x9f) > 3) /* priority <= 3 */ | |
499 fprintf(stderr, "sanity check failed!\n"); | |
500 id = get_v(bc); | |
501 timestamp = get_s(bc); | |
502 | |
503 size = (nut->curr_frame_size - (url_ftell(bc)-nut->curr_frame_start)); | |
504 dprintf("flags: 0x%x, timestamp: %d, packet size: %d\n", tmp, timestamp, size); | |
505 | |
506 if (size < 0) | |
507 return -1; | |
508 | |
509 av_new_packet(pkt, size); | |
510 get_buffer(bc, pkt->data, size); | |
511 pkt->stream_index = id; | |
512 if (key_frame) | |
513 pkt->flags |= PKT_FLAG_KEY; | |
514 pkt->pts = timestamp; | |
515 | |
516 return 0; | |
517 } | |
518 | |
519 static AVInputFormat nut_iformat = { | |
520 "nut", | |
521 "nut format", | |
522 sizeof(NUTContext), | |
523 nut_probe, | |
524 nut_read_header, | |
525 nut_read_packet, | |
526 // nut_read_close, | |
527 // nut_read_seek, | |
528 .extensions = "nut", | |
529 }; | |
530 | |
531 static AVOutputFormat nut_oformat = { | |
532 "nut", | |
533 "nut format", | |
534 "video/x-nut", | |
535 "nut", | |
536 sizeof(NUTContext), | |
537 #ifdef CONFIG_VORBIS | |
538 CODEC_ID_VORBIS, | |
539 #elif defined(CONFIG_MP3LAME) | |
540 CODEC_ID_MP3LAME, | |
541 #else | |
542 CODEC_ID_MP2, | |
543 #endif | |
544 CODEC_ID_MPEG4, | |
545 nut_write_header, | |
546 nut_write_packet, | |
547 nut_write_trailer, | |
548 }; | |
549 | |
550 int nut_init(void) | |
551 { | |
552 av_register_input_format(&nut_iformat); | |
553 av_register_output_format(&nut_oformat); | |
554 return 0; | |
555 } |