comparison avienc.c @ 119:602546f3cbea libavformat

Complete support for OpenDML AVIs and AVIs > 2Gb.
author romansh
date Wed, 23 Apr 2003 02:04:40 +0000
parents 4da5d55b3690
children c720ddf380df
comparison
equal deleted inserted replaced
118:79d651162d35 119:602546f3cbea
22 /* 22 /*
23 * TODO: 23 * TODO:
24 * - fill all fields if non streamed (nb_frames for example) 24 * - fill all fields if non streamed (nb_frames for example)
25 */ 25 */
26 26
27 typedef struct AVIIentry {
28 unsigned int flags, pos, len;
29 } AVIIentry;
30
31 #define AVI_INDEX_CLUSTER_SIZE 16384
32
27 typedef struct AVIIndex { 33 typedef struct AVIIndex {
28 unsigned char tag[4]; 34 offset_t indx_start;
29 unsigned int flags, pos, len; 35 int entry;
30 struct AVIIndex *next; 36 int ents_allocated;
37 AVIIentry** cluster;
31 } AVIIndex; 38 } AVIIndex;
32 39
33 typedef struct { 40 typedef struct {
34 offset_t riff_start, movi_list, odml_list; 41 offset_t riff_start, movi_list, odml_list;
35 offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS]; 42 offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
36 int audio_strm_length[MAX_STREAMS]; 43 int audio_strm_length[MAX_STREAMS];
37 AVIIndex *first, *last; 44 int riff_id;
45
46 AVIIndex indexes[MAX_STREAMS];
38 } AVIContext; 47 } AVIContext;
48
49 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
50 {
51 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
52 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
53 return &idx->cluster[cl][id];
54 }
39 55
40 offset_t start_tag(ByteIOContext *pb, const char *tag) 56 offset_t start_tag(ByteIOContext *pb, const char *tag)
41 { 57 {
42 put_tag(pb, tag); 58 put_tag(pb, tag);
43 put_le32(pb, 0); 59 put_le32(pb, 0);
238 254
239 static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb, 255 static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
240 const char* riff_tag, const char* list_tag) 256 const char* riff_tag, const char* list_tag)
241 { 257 {
242 offset_t loff; 258 offset_t loff;
259 int i;
260
261 avi->riff_id++;
262 for (i=0; i<MAX_STREAMS; i++)
263 avi->indexes[i].entry = 0;
264
243 avi->riff_start = start_tag(pb, "RIFF"); 265 avi->riff_start = start_tag(pb, "RIFF");
244 put_tag(pb, riff_tag); 266 put_tag(pb, riff_tag);
245 loff = start_tag(pb, "LIST"); 267 loff = start_tag(pb, "LIST");
246 put_tag(pb, list_tag); 268 put_tag(pb, list_tag);
247 return loff; 269 return loff;
248 } 270 }
249 271
272 static unsigned char* avi_stream2fourcc(unsigned char* tag, int index,
273 enum CodecType type)
274 {
275 tag[0] = '0';
276 tag[1] = '0' + index;
277 if (type == CODEC_TYPE_VIDEO) {
278 tag[2] = 'd';
279 tag[3] = 'c';
280 } else {
281 tag[2] = 'w';
282 tag[3] = 'b';
283 }
284 tag[4] = '\0';
285 return tag;
286 }
287
250 static int avi_write_header(AVFormatContext *s) 288 static int avi_write_header(AVFormatContext *s)
251 { 289 {
252 AVIContext *avi = s->priv_data; 290 AVIContext *avi = s->priv_data;
253 ByteIOContext *pb = &s->pb; 291 ByteIOContext *pb = &s->pb;
254 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale; 292 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
255 AVCodecContext *stream, *video_enc; 293 AVCodecContext *stream, *video_enc;
256 offset_t list1, list2, strh, strf; 294 offset_t list1, list2, strh, strf;
257 295
258 /* header list */ 296 /* header list */
297 avi->riff_id = 0;
259 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl"); 298 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
260 299
261 /* avi header */ 300 /* avi header */
262 put_tag(pb, "avih"); 301 put_tag(pb, "avih");
263 put_le32(pb, 14 * 4); 302 put_le32(pb, 14 * 4);
372 break; 411 break;
373 default: 412 default:
374 av_abort(); 413 av_abort();
375 } 414 }
376 end_tag(pb, strf); 415 end_tag(pb, strf);
416
417 if (!url_is_streamed(pb)) {
418 unsigned char tag[5];
419
420 /* Starting to lay out AVI OpenDML master index.
421 * We want to make it JUNK entry for now, since we'd
422 * like to get away without making AVI and OpenDML one
423 * for compatibility reasons.
424 */
425 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
426 avi->indexes[i].indx_start = start_tag(pb, "JUNK");
427 put_le16(pb, 4); /* wLongsPerEntry */
428 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
429 put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
430 put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
431 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
432 /* dwChunkId */
433 put_le64(pb, 0); /* dwReserved[3]
434 put_le32(pb, 0); Must be 0. */
435 url_fskip(pb, AVI_MASTER_INDEX_SIZE * 4 * 4);
436 end_tag(pb, avi->indexes[i].indx_start);
437 }
438
377 end_tag(pb, list2); 439 end_tag(pb, list2);
378 } 440 }
379 441
380 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */ 442 if (!url_is_streamed(pb)) {
381 avi->odml_list = start_tag(pb, "JUNK"); 443 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
382 put_tag(pb, "odml"); 444 avi->odml_list = start_tag(pb, "JUNK");
383 put_tag(pb, "dmlh"); 445 put_tag(pb, "odml");
384 put_le32(pb, 248); 446 put_tag(pb, "dmlh");
385 for (i = 0; i < 248; i+= 4) 447 put_le32(pb, 248);
386 put_le32(pb, 0); 448 for (i = 0; i < 248; i+= 4)
387 end_tag(pb, avi->odml_list); 449 put_le32(pb, 0);
450 end_tag(pb, avi->odml_list);
451 }
388 452
389 end_tag(pb, list1); 453 end_tag(pb, list1);
390 454
391 avi->movi_list = start_tag(pb, "LIST"); 455 avi->movi_list = start_tag(pb, "LIST");
392 avi->first = NULL;
393 avi->last = NULL;
394 put_tag(pb, "movi"); 456 put_tag(pb, "movi");
395 457
396 put_flush_packet(pb); 458 put_flush_packet(pb);
397 459
398 return 0; 460 return 0;
399 } 461 }
400 462
401 static int avi_finish_riff1(AVFormatContext *s) 463 static int avi_write_ix(AVFormatContext *s)
464 {
465 ByteIOContext *pb = &s->pb;
466 AVIContext *avi = s->priv_data;
467 unsigned char tag[5];
468 unsigned char ix_tag[] = "ix00";
469 int i, j;
470
471 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
472 return -1;
473
474 for (i=0;i<s->nb_streams;i++) {
475 offset_t ix, pos;
476
477 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec.codec_type);
478 ix_tag[3] = '0' + i;
479
480 /* Writing AVI OpenDML leaf index chunk */
481 ix = url_ftell(pb);
482 put_tag(pb, &ix_tag[0]); /* ix?? */
483 put_le32(pb, avi->indexes[i].entry * 8 + 24);
484 /* chunk size */
485 put_le16(pb, 2); /* wLongsPerEntry */
486 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
487 put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
488 put_le32(pb, avi->indexes[i].entry);
489 /* nEntriesInUse */
490 put_tag(pb, &tag[0]); /* dwChunkId */
491 put_le64(pb, avi->movi_list);/* qwBaseOffset */
492 put_le32(pb, 0); /* dwReserved_3 (must be 0) */
493
494 for (j=0; j<avi->indexes[i].entry; j++) {
495 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
496 put_le32(pb, ie->pos + 8);
497 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
498 (ie->flags & 0x10 ? 0 : 0x80000000));
499 }
500 put_flush_packet(pb);
501 pos = url_ftell(pb);
502
503 /* Updating one entry in the AVI OpenDML master index */
504 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
505 put_tag(pb, "indx"); /* enabling this entry */
506 url_fskip(pb, 8);
507 put_le32(pb, avi->riff_id); /* nEntriesInUse */
508 url_fskip(pb, 16*avi->riff_id);
509 put_le64(pb, ix); /* qwOffset */
510 put_le32(pb, pos - ix); /* dwSize */
511 put_le32(pb, avi->indexes[i].entry); /* dwDuration */
512
513 url_fseek(pb, pos, SEEK_SET);
514 }
515 return 0;
516 }
517
518 static int avi_write_idx1(AVFormatContext *s)
402 { 519 {
403 ByteIOContext *pb = &s->pb; 520 ByteIOContext *pb = &s->pb;
404 AVIContext *avi = s->priv_data; 521 AVIContext *avi = s->priv_data;
405 offset_t file_size, idx_chunk; 522 offset_t file_size, idx_chunk;
406 int n, nb_frames, au_byterate, au_ssize, au_scale; 523 int i, n, nb_frames, au_byterate, au_ssize, au_scale;
407 AVCodecContext *stream; 524 AVCodecContext *stream;
408 AVIIndex *idx; 525 unsigned char tag[5];
409 526
410 if (!url_is_streamed(pb)) { 527 if (!url_is_streamed(pb)) {
411 end_tag(pb, avi->movi_list); 528 AVIIentry* ie, *tie;
412 529 int entry[MAX_STREAMS];
413 idx_chunk = start_tag(pb, "idx1"); 530 int empty, stream_id;
414 idx = avi->first; 531
415 while (idx != NULL) { 532 idx_chunk = start_tag(pb, "idx1");
416 put_buffer(pb, idx->tag, 4); 533 memset(&entry[0], 0, sizeof(entry));
417 put_le32(pb, idx->flags); 534 do {
418 put_le32(pb, idx->pos); 535 empty = 1;
419 put_le32(pb, idx->len); 536 for (i=0; i<s->nb_streams; i++) {
420 idx = idx->next; 537 if (avi->indexes[i].entry <= entry[i])
421 } 538 continue;
422 end_tag(pb, idx_chunk); 539
423 540 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
424 /* update file size */ 541 if (empty || tie->pos < ie->pos) {
542 ie = tie;
543 stream_id = i;
544 }
545 empty = 0;
546 }
547 if (!empty) {
548 avi_stream2fourcc(&tag[0], stream_id,
549 s->streams[stream_id]->codec.codec_type);
550 put_tag(pb, &tag[0]);
551 put_le32(pb, ie->flags);
552 put_le32(pb, ie->pos);
553 put_le32(pb, ie->len);
554 entry[stream_id]++;
555 }
556 } while (!empty);
557 end_tag(pb, idx_chunk);
558
559 /* Fill in frame/sample counters */
425 file_size = url_ftell(pb); 560 file_size = url_ftell(pb);
426 end_tag(pb, avi->riff_start);
427
428 /* Fill in frame/sample counters */
429 nb_frames = 0; 561 nb_frames = 0;
430 for(n=0;n<s->nb_streams;n++) { 562 for(n=0;n<s->nb_streams;n++) {
431 if (avi->frames_hdr_strm[n] != 0) { 563 if (avi->frames_hdr_strm[n] != 0) {
432 stream = &s->streams[n]->codec; 564 stream = &s->streams[n]->codec;
433 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET); 565 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
458 static int avi_write_packet(AVFormatContext *s, int stream_index, 590 static int avi_write_packet(AVFormatContext *s, int stream_index,
459 uint8_t *buf, int size, int force_pts) 591 uint8_t *buf, int size, int force_pts)
460 { 592 {
461 AVIContext *avi = s->priv_data; 593 AVIContext *avi = s->priv_data;
462 ByteIOContext *pb = &s->pb; 594 ByteIOContext *pb = &s->pb;
463 AVIIndex *idx;
464 unsigned char tag[5]; 595 unsigned char tag[5];
465 unsigned int flags; 596 unsigned int flags;
466 AVCodecContext *enc; 597 AVCodecContext *enc;
467 598
468 if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) { 599 if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) {
469 if (avi->riff_start != 8) { 600 avi_write_ix(s);
470 end_tag(pb, avi->movi_list); 601 end_tag(pb, avi->movi_list);
471 end_tag(pb, avi->riff_start); 602
472 } else 603 if (avi->riff_id == 1)
473 avi_finish_riff1(s); 604 avi_write_idx1(s);
474 605
606 end_tag(pb, avi->riff_start);
475 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi"); 607 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
476 } 608 }
477 609
478 enc = &s->streams[stream_index]->codec; 610 enc = &s->streams[stream_index]->codec;
479 611 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
480 tag[0] = '0'; 612 if (enc->codec_type == CODEC_TYPE_AUDIO) {
481 tag[1] = '0' + stream_index;
482 if (enc->codec_type == CODEC_TYPE_VIDEO) {
483 tag[2] = 'd';
484 tag[3] = 'c';
485 flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
486 } else {
487 tag[2] = 'w';
488 tag[3] = 'b';
489 flags = 0x10;
490 }
491 if (enc->codec_type == CODEC_TYPE_AUDIO)
492 avi->audio_strm_length[stream_index] += size; 613 avi->audio_strm_length[stream_index] += size;
614 flags = 0x10;
615 } else
616 flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
493 617
494 if (!url_is_streamed(&s->pb)) { 618 if (!url_is_streamed(&s->pb)) {
495 idx = av_malloc(sizeof(AVIIndex)); 619 AVIIndex* idx = &avi->indexes[stream_index];
496 memcpy(idx->tag, tag, 4); 620 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
497 idx->flags = flags; 621 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
498 idx->pos = url_ftell(pb) - avi->movi_list; 622 if (idx->ents_allocated <= idx->entry) {
499 idx->len = size; 623 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
500 idx->next = NULL; 624 if (!idx->cluster)
501 if (!avi->last) 625 return -1;
502 avi->first = idx; 626 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
503 else 627 if (!idx->cluster[cl])
504 avi->last->next = idx; 628 return -1;
505 avi->last = idx; 629 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
630 }
631
632 idx->cluster[cl][id].flags = flags;
633 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
634 idx->cluster[cl][id].len = size;
635 idx->entry++;
506 } 636 }
507 637
508 put_buffer(pb, tag, 4); 638 put_buffer(pb, tag, 4);
509 put_le32(pb, size); 639 put_le32(pb, size);
510 put_buffer(pb, buf, size); 640 put_buffer(pb, buf, size);
518 static int avi_write_trailer(AVFormatContext *s) 648 static int avi_write_trailer(AVFormatContext *s)
519 { 649 {
520 AVIContext *avi = s->priv_data; 650 AVIContext *avi = s->priv_data;
521 ByteIOContext *pb = &s->pb; 651 ByteIOContext *pb = &s->pb;
522 int res = 0; 652 int res = 0;
523 653 int i, j, n, nb_frames;
524 if (avi->riff_start != 8) { 654 offset_t file_size;
525 int n, nb_frames; 655
526 offset_t file_size; 656 if (avi->riff_id == 1) {
527 657 end_tag(pb, avi->movi_list);
658 res = avi_write_idx1(s);
659 end_tag(pb, avi->riff_start);
660 } else {
661 avi_write_ix(s);
662 end_tag(pb, avi->movi_list);
663 end_tag(pb, avi->riff_start);
664
528 file_size = url_ftell(pb); 665 file_size = url_ftell(pb);
529 url_fseek(pb, avi->odml_list - 8, SEEK_SET); 666 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
530 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */ 667 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
531 url_fskip(pb, 16); 668 url_fskip(pb, 16);
532 669
540 nb_frames += stream->frame_number; 677 nb_frames += stream->frame_number;
541 } 678 }
542 } 679 }
543 } 680 }
544 put_le32(pb, nb_frames); 681 put_le32(pb, nb_frames);
545
546 end_tag(pb, avi->movi_list);
547 end_tag(pb, avi->riff_start);
548 url_fseek(pb, file_size, SEEK_SET); 682 url_fseek(pb, file_size, SEEK_SET);
549 } else 683 }
550 res = avi_finish_riff1(s);
551
552 put_flush_packet(pb); 684 put_flush_packet(pb);
685
686 for (i=0; i<MAX_STREAMS; i++) {
687 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
688 av_free(avi->indexes[i].cluster[j]);
689 av_free(avi->indexes[i].cluster);
690 avi->indexes[i].cluster = NULL;
691 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
692 }
693
553 return res; 694 return res;
554 } 695 }
555 696
556 static AVOutputFormat avi_oformat = { 697 static AVOutputFormat avi_oformat = {
557 "avi", 698 "avi",