Browse code

Complete support for OpenDML AVIs and AVIs > 2Gb.

Originally committed as revision 1814 to svn://svn.ffmpeg.org/ffmpeg/trunk

Roman Shaposhnik authored on 2003/04/23 11:04:40
Showing 6 changed files
... ...
@@ -1363,9 +1363,9 @@ static int av_encode(AVFormatContext **output_files,
1363 1363
                         /* no reencoding needed : output the packet directly */
1364 1364
                         /* force the input stream PTS */
1365 1365
                         
1366
-                        //XXX/FIXME set keyframe flag from demuxer (or optionally from decoder)
1367 1366
                         memset(&avframe, 0, sizeof(AVFrame));
1368 1367
                         ost->st->codec.coded_frame= &avframe;
1368
+			avframe.key_frame = pkt.flags & PKT_FLAG_KEY; 
1369 1369
                         
1370 1370
                         av_write_frame(os, ost->index, data_buf, data_size);
1371 1371
 			ost->st->codec.frame_number++;
... ...
@@ -7,6 +7,7 @@
7 7
 #define AVIF_COPYRIGHTED	0x00020000
8 8
 
9 9
 #define AVI_MAX_RIFF_SIZE       0x40000000LL
10
+#define AVI_MASTER_INDEX_SIZE   256
10 11
 
11 12
 offset_t start_tag(ByteIOContext *pb, const char *tag);
12 13
 void end_tag(ByteIOContext *pb, offset_t start);
... ...
@@ -368,6 +368,7 @@ pkt_init:
368 368
     pkt->size = avi->buf_size;
369 369
     pkt->destruct = __destruct_pkt;
370 370
     pkt->stream_index = avi->stream_index;
371
+    pkt->flags |= PKT_FLAG_KEY; // FIXME: We really should read index for that
371 372
     avi->stream_index = !avi->stream_index;
372 373
     return 0;
373 374
 }
... ...
@@ -24,19 +24,35 @@
24 24
  *  - fill all fields if non streamed (nb_frames for example)
25 25
  */
26 26
 
27
-typedef struct AVIIndex {
28
-    unsigned char tag[4];
27
+typedef struct AVIIentry {
29 28
     unsigned int flags, pos, len;
30
-    struct AVIIndex *next;
29
+} AVIIentry;
30
+
31
+#define AVI_INDEX_CLUSTER_SIZE 16384
32
+
33
+typedef struct AVIIndex {
34
+    offset_t    indx_start;
35
+    int         entry;
36
+    int         ents_allocated;
37
+    AVIIentry** cluster;
31 38
 } AVIIndex;
32 39
 
33 40
 typedef struct {
34 41
     offset_t riff_start, movi_list, odml_list;
35 42
     offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
36 43
     int audio_strm_length[MAX_STREAMS];
37
-    AVIIndex *first, *last;
44
+    int riff_id;
45
+
46
+    AVIIndex indexes[MAX_STREAMS];
38 47
 } AVIContext;
39 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
+}
55
+
40 56
 offset_t start_tag(ByteIOContext *pb, const char *tag)
41 57
 {
42 58
     put_tag(pb, tag);
... ...
@@ -240,6 +256,12 @@ static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
240 240
                                    const char* riff_tag, const char* list_tag)
241 241
 {
242 242
     offset_t loff;
243
+    int i;
244
+    
245
+    avi->riff_id++;
246
+    for (i=0; i<MAX_STREAMS; i++)
247
+         avi->indexes[i].entry = 0;
248
+    
243 249
     avi->riff_start = start_tag(pb, "RIFF");
244 250
     put_tag(pb, riff_tag);
245 251
     loff = start_tag(pb, "LIST");
... ...
@@ -247,6 +269,22 @@ static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
247 247
     return loff;
248 248
 }
249 249
 
250
+static unsigned char* avi_stream2fourcc(unsigned char* tag, int index, 
251
+                                        enum CodecType type)
252
+{
253
+    tag[0] = '0';
254
+    tag[1] = '0' + index;
255
+    if (type == CODEC_TYPE_VIDEO) {
256
+        tag[2] = 'd';
257
+        tag[3] = 'c';
258
+    } else {
259
+        tag[2] = 'w';
260
+        tag[3] = 'b';
261
+    }
262
+    tag[4] = '\0';
263
+    return tag;
264
+}
265
+
250 266
 static int avi_write_header(AVFormatContext *s)
251 267
 {
252 268
     AVIContext *avi = s->priv_data;
... ...
@@ -256,6 +294,7 @@ static int avi_write_header(AVFormatContext *s)
256 256
     offset_t list1, list2, strh, strf;
257 257
 
258 258
     /* header list */
259
+    avi->riff_id = 0;
259 260
     list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
260 261
 
261 262
     /* avi header */
... ...
@@ -374,23 +413,46 @@ static int avi_write_header(AVFormatContext *s)
374 374
             av_abort();
375 375
         }
376 376
         end_tag(pb, strf);
377
+	
378
+	if (!url_is_streamed(pb)) {
379
+	    unsigned char tag[5];
380
+    
381
+            /* Starting to lay out AVI OpenDML master index. 
382
+	     * We want to make it JUNK entry for now, since we'd
383
+	     * like to get away without making AVI and OpenDML one 
384
+	     * for compatibility reasons.
385
+	     */
386
+	    avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
387
+	    avi->indexes[i].indx_start = start_tag(pb, "JUNK"); 
388
+	    put_le16(pb, 4);        /* wLongsPerEntry */
389
+	    put_byte(pb, 0);        /* bIndexSubType (0 == frame index) */
390
+	    put_byte(pb, 0);        /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
391
+	    put_le32(pb, 0);        /* nEntriesInUse (will fill out later on) */
392
+	    put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
393
+	                            /* dwChunkId */
394
+	    put_le64(pb, 0);        /* dwReserved[3]
395
+	    put_le32(pb, 0);           Must be 0.    */
396
+	    url_fskip(pb, AVI_MASTER_INDEX_SIZE * 4 * 4);
397
+	    end_tag(pb, avi->indexes[i].indx_start);
398
+	}
399
+	
377 400
         end_tag(pb, list2);
378 401
     }
379 402
     
380
-    /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
381
-    avi->odml_list = start_tag(pb, "JUNK");
382
-    put_tag(pb, "odml");
383
-    put_tag(pb, "dmlh");
384
-    put_le32(pb, 248);
385
-    for (i = 0; i < 248; i+= 4)
386
-        put_le32(pb, 0);
387
-    end_tag(pb, avi->odml_list);
403
+    if (!url_is_streamed(pb)) {
404
+        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
405
+        avi->odml_list = start_tag(pb, "JUNK");
406
+        put_tag(pb, "odml");
407
+        put_tag(pb, "dmlh");
408
+        put_le32(pb, 248);
409
+        for (i = 0; i < 248; i+= 4)
410
+             put_le32(pb, 0);
411
+        end_tag(pb, avi->odml_list);
412
+    }
388 413
 
389 414
     end_tag(pb, list1);
390 415
     
391 416
     avi->movi_list = start_tag(pb, "LIST");
392
-    avi->first = NULL;
393
-    avi->last = NULL;
394 417
     put_tag(pb, "movi");
395 418
 
396 419
     put_flush_packet(pb);
... ...
@@ -398,34 +460,104 @@ static int avi_write_header(AVFormatContext *s)
398 398
     return 0;
399 399
 }
400 400
 
401
-static int avi_finish_riff1(AVFormatContext *s)
401
+static int avi_write_ix(AVFormatContext *s)
402
+{
403
+    ByteIOContext *pb = &s->pb;
404
+    AVIContext *avi = s->priv_data;
405
+    unsigned char tag[5];
406
+    unsigned char ix_tag[] = "ix00";
407
+    int i, j;
408
+    
409
+    if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
410
+        return -1;
411
+    
412
+    for (i=0;i<s->nb_streams;i++) {
413
+	 offset_t ix, pos;
414
+	 
415
+	 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec.codec_type);
416
+	 ix_tag[3] = '0' + i;
417
+	 
418
+	 /* Writing AVI OpenDML leaf index chunk */
419
+	 ix = url_ftell(pb); 
420
+	 put_tag(pb, &ix_tag[0]);     /* ix?? */
421
+	 put_le32(pb, avi->indexes[i].entry * 8 + 24); 
422
+	                              /* chunk size */
423
+         put_le16(pb, 2);             /* wLongsPerEntry */
424
+	 put_byte(pb, 0);             /* bIndexSubType (0 == frame index) */ 
425
+	 put_byte(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
426
+	 put_le32(pb, avi->indexes[i].entry);          
427
+	                              /* nEntriesInUse */
428
+	 put_tag(pb, &tag[0]);        /* dwChunkId */
429
+	 put_le64(pb, avi->movi_list);/* qwBaseOffset */
430
+	 put_le32(pb, 0);             /* dwReserved_3 (must be 0) */
431
+
432
+         for (j=0; j<avi->indexes[i].entry; j++) {
433
+             AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
434
+	     put_le32(pb, ie->pos + 8);
435
+	     put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
436
+	                  (ie->flags & 0x10 ? 0 : 0x80000000));
437
+         }
438
+	 put_flush_packet(pb);
439
+         pos = url_ftell(pb);
440
+	
441
+	 /* Updating one entry in the AVI OpenDML master index */
442
+	 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
443
+	 put_tag(pb, "indx");                 /* enabling this entry */
444
+	 url_fskip(pb, 8);
445
+	 put_le32(pb, avi->riff_id);          /* nEntriesInUse */
446
+	 url_fskip(pb, 16*avi->riff_id);
447
+	 put_le64(pb, ix);                    /* qwOffset */
448
+	 put_le32(pb, pos - ix);              /* dwSize */
449
+	 put_le32(pb, avi->indexes[i].entry); /* dwDuration */
450
+
451
+	 url_fseek(pb, pos, SEEK_SET);
452
+    }
453
+    return 0;
454
+}
455
+
456
+static int avi_write_idx1(AVFormatContext *s)
402 457
 {
403 458
     ByteIOContext *pb = &s->pb;
404 459
     AVIContext *avi = s->priv_data;
405 460
     offset_t file_size, idx_chunk;
406
-    int n, nb_frames, au_byterate, au_ssize, au_scale;
461
+    int i, n, nb_frames, au_byterate, au_ssize, au_scale;
407 462
     AVCodecContext *stream;
408
-    AVIIndex *idx;
463
+    unsigned char tag[5];
409 464
 
410 465
     if (!url_is_streamed(pb)) {
411
-        end_tag(pb, avi->movi_list);
412
-
413
-        idx_chunk = start_tag(pb, "idx1");
414
-        idx = avi->first;
415
-        while (idx != NULL) {
416
-            put_buffer(pb, idx->tag, 4);
417
-            put_le32(pb, idx->flags);
418
-            put_le32(pb, idx->pos);
419
-            put_le32(pb, idx->len);
420
-            idx = idx->next;
421
-        }
422
-        end_tag(pb, idx_chunk);
423
-        
424
-        /* update file size */
425
-	file_size = url_ftell(pb);
426
-	end_tag(pb, avi->riff_start);
466
+	AVIIentry* ie, *tie;
467
+	int entry[MAX_STREAMS];
468
+	int empty, stream_id;
469
+
470
+	idx_chunk = start_tag(pb, "idx1");
471
+	memset(&entry[0], 0, sizeof(entry));
472
+	do {
473
+	    empty = 1;
474
+	    for (i=0; i<s->nb_streams; i++) {
475
+	         if (avi->indexes[i].entry <= entry[i])
476
+		     continue;
477
+		 
478
+		 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
479
+		 if (empty || tie->pos < ie->pos) {
480
+		     ie = tie; 
481
+		     stream_id = i;
482
+		 }
483
+		 empty = 0;
484
+	    }
485
+	    if (!empty) {
486
+	        avi_stream2fourcc(&tag[0], stream_id, 
487
+		                  s->streams[stream_id]->codec.codec_type); 
488
+	        put_tag(pb, &tag[0]);
489
+		put_le32(pb, ie->flags);
490
+                put_le32(pb, ie->pos);
491
+                put_le32(pb, ie->len);
492
+		entry[stream_id]++;
493
+	    }
494
+	} while (!empty);
495
+	end_tag(pb, idx_chunk);
427 496
 
428 497
         /* Fill in frame/sample counters */
498
+	file_size = url_ftell(pb);
429 499
         nb_frames = 0;
430 500
         for(n=0;n<s->nb_streams;n++) {
431 501
             if (avi->frames_hdr_strm[n] != 0) {
... ...
@@ -460,49 +592,47 @@ static int avi_write_packet(AVFormatContext *s, int stream_index,
460 460
 {
461 461
     AVIContext *avi = s->priv_data;
462 462
     ByteIOContext *pb = &s->pb;
463
-    AVIIndex *idx;
464 463
     unsigned char tag[5];
465 464
     unsigned int flags;
466 465
     AVCodecContext *enc;
467 466
 
468 467
     if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) { 
469
-        if (avi->riff_start != 8) {
470
-	    end_tag(pb, avi->movi_list);
471
-	    end_tag(pb, avi->riff_start);
472
-	} else
473
-	    avi_finish_riff1(s);
474
-	
468
+        avi_write_ix(s);
469
+        end_tag(pb, avi->movi_list);
470
+        
471
+	if (avi->riff_id == 1)
472
+	    avi_write_idx1(s);
473
+
474
+	end_tag(pb, avi->riff_start);
475 475
 	avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
476 476
     }
477 477
     
478 478
     enc = &s->streams[stream_index]->codec;
479
-
480
-    tag[0] = '0';
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) 
479
+    avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
480
+    if (enc->codec_type == CODEC_TYPE_AUDIO) {
492 481
        avi->audio_strm_length[stream_index] += size;
482
+       flags = 0x10;
483
+    } else
484
+       flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
493 485
 
494 486
     if (!url_is_streamed(&s->pb)) {
495
-        idx = av_malloc(sizeof(AVIIndex));
496
-        memcpy(idx->tag, tag, 4);
497
-        idx->flags = flags;
498
-        idx->pos = url_ftell(pb) - avi->movi_list;
499
-        idx->len = size;
500
-        idx->next = NULL;
501
-        if (!avi->last)
502
-            avi->first = idx;
503
-        else
504
-            avi->last->next = idx;
505
-        avi->last = idx;
487
+        AVIIndex* idx = &avi->indexes[stream_index];
488
+	int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
489
+	int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
490
+        if (idx->ents_allocated <= idx->entry) {
491
+	    idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*)); 
492
+	    if (!idx->cluster)
493
+	        return -1;
494
+            idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
495
+	    if (!idx->cluster[cl])
496
+		return -1;
497
+	    idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
498
+	}
499
+ 	
500
+	idx->cluster[cl][id].flags = flags; 
501
+        idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
502
+        idx->cluster[cl][id].len = size;
503
+	idx->entry++;
506 504
     }
507 505
     
508 506
     put_buffer(pb, tag, 4);
... ...
@@ -520,11 +650,18 @@ static int avi_write_trailer(AVFormatContext *s)
520 520
     AVIContext *avi = s->priv_data;
521 521
     ByteIOContext *pb = &s->pb;
522 522
     int res = 0;
523
+    int i, j, n, nb_frames;
524
+    offset_t file_size;
525
+
526
+    if (avi->riff_id == 1) {
527
+        end_tag(pb, avi->movi_list);
528
+        res = avi_write_idx1(s);
529
+	end_tag(pb, avi->riff_start);
530
+    } else {
531
+        avi_write_ix(s);
532
+        end_tag(pb, avi->movi_list);
533
+	end_tag(pb, avi->riff_start);
523 534
 
524
-    if (avi->riff_start != 8) {
525
-        int n, nb_frames;
526
-	offset_t file_size;
527
-	
528 535
         file_size = url_ftell(pb);
529 536
 	url_fseek(pb, avi->odml_list - 8, SEEK_SET);
530 537
 	put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
... ...
@@ -542,14 +679,18 @@ static int avi_write_trailer(AVFormatContext *s)
542 542
             }
543 543
         }
544 544
 	put_le32(pb, nb_frames);
545
-
546
-	end_tag(pb, avi->movi_list);
547
-	end_tag(pb, avi->riff_start);
548 545
 	url_fseek(pb, file_size, SEEK_SET);
549
-    } else
550
-        res = avi_finish_riff1(s);
551
-
546
+    }
552 547
     put_flush_packet(pb);
548
+
549
+    for (i=0; i<MAX_STREAMS; i++) {
550
+	 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
551
+              av_free(avi->indexes[i].cluster[j]);
552
+	 av_free(avi->indexes[i].cluster);
553
+	 avi->indexes[i].cluster = NULL;
554
+	 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
555
+    }
556
+    
553 557
     return res;
554 558
 }
555 559
 
... ...
@@ -83,6 +83,7 @@ static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
83 83
     pkt->data     = c->buf;
84 84
     pkt->size     = c->size;
85 85
     pkt->stream_index = c->is_audio;
86
+    pkt->flags   |= PKT_FLAG_KEY;
86 87
     
87 88
     c->is_audio = !c->is_audio;
88 89
     return c->size;
... ...
@@ -184,6 +184,7 @@ static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt)
184 184
     pkt->size     = dv->frame_size;
185 185
     pkt->pts      = dv->pts;
186 186
     pkt->stream_index = dv->stream;
187
+    pkt->flags   |= PKT_FLAG_KEY;
187 188
 
188 189
     dv->stream ^= 1;
189 190