Browse code

wav: add an option for writing BEXT chunk

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Benjamin Larsson authored on 2011/03/18 09:00:15
Showing 3 changed files
... ...
@@ -2,6 +2,10 @@ Entries are sorted chronologically from oldest to youngest within each release,
2 2
 releases are sorted from youngest to oldest.
3 3
 
4 4
 
5
+version <next>:
6
+- BWF muxer
7
+
8
+
5 9
 version 0.7:
6 10
 
7 11
 - E-AC-3 audio encoder
... ...
@@ -66,6 +66,7 @@ library:
66 66
     @tab Used in Z and Z95 games.
67 67
 @item Brute Force & Ignorance   @tab   @tab X
68 68
     @tab Used in the game Flash Traffic: City of Angels.
69
+@item BWF                       @tab X @tab
69 70
 @item Interplay C93             @tab   @tab X
70 71
     @tab Used in the game Cyberia from Interplay.
71 72
 @item Delphine Software International CIN @tab   @tab X
... ...
@@ -23,22 +23,82 @@
23 23
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 24
  */
25 25
 
26
+#include "libavutil/log.h"
26 27
 #include "libavutil/mathematics.h"
28
+#include "libavutil/opt.h"
27 29
 #include "avformat.h"
28 30
 #include "avio_internal.h"
29 31
 #include "pcm.h"
30 32
 #include "riff.h"
33
+#include "avio.h"
34
+#include "avio_internal.h"
31 35
 
32 36
 typedef struct {
37
+    const AVClass *class;
33 38
     int64_t data;
34 39
     int64_t data_end;
35 40
     int64_t minpts;
36 41
     int64_t maxpts;
37 42
     int last_duration;
38 43
     int w64;
44
+    int write_bext;
39 45
 } WAVContext;
40 46
 
41 47
 #if CONFIG_WAV_MUXER
48
+static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
49
+{
50
+    AVDictionaryEntry *tag;
51
+    int len = 0;
52
+
53
+    if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
54
+        len = strlen(tag->value);
55
+        len = FFMIN(len, maxlen);
56
+        avio_write(s->pb, tag->value, len);
57
+    }
58
+
59
+    ffio_fill(s->pb, 0, maxlen - len);
60
+}
61
+
62
+static void bwf_write_bext_chunk(AVFormatContext *s)
63
+{
64
+    AVDictionaryEntry *tmp_tag;
65
+    uint64_t time_reference = 0;
66
+    int64_t bext = ff_start_tag(s->pb, "bext");
67
+
68
+    bwf_write_bext_string(s, "description", 256);
69
+    bwf_write_bext_string(s, "originator", 32);
70
+    bwf_write_bext_string(s, "originator_reference", 32);
71
+    bwf_write_bext_string(s, "origination_date", 10);
72
+    bwf_write_bext_string(s, "origination_time", 8);
73
+
74
+    if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
75
+        time_reference = strtoll(tmp_tag->value, NULL, 10);
76
+    avio_wl64(s->pb, time_reference);
77
+    avio_wl16(s->pb, 1);  // set version to 1
78
+
79
+    if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
80
+        unsigned char umidpart_str[17] = {0};
81
+        int i;
82
+        uint64_t umidpart;
83
+        int len = strlen(tmp_tag->value+2);
84
+
85
+        for (i = 0; i < len/16; i++) {
86
+            memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
87
+            umidpart = strtoll(umidpart_str, NULL, 16);
88
+            avio_wb64(s->pb, umidpart);
89
+        }
90
+        ffio_fill(s->pb, 0, 64 - i*8);
91
+    } else
92
+        ffio_fill(s->pb, 0, 64); // zero UMID
93
+
94
+    ffio_fill(s->pb, 0, 190); // Reserved
95
+
96
+    if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
97
+        avio_put_str(s->pb, tmp_tag->value);
98
+
99
+    ff_end_tag(s->pb, bext);
100
+}
101
+
42 102
 static int wav_write_header(AVFormatContext *s)
43 103
 {
44 104
     WAVContext *wav = s->priv_data;
... ...
@@ -65,6 +125,9 @@ static int wav_write_header(AVFormatContext *s)
65 65
         ff_end_tag(pb, fact);
66 66
     }
67 67
 
68
+    if (wav->write_bext)
69
+        bwf_write_bext_chunk(s);
70
+
68 71
     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
69 72
     wav->maxpts = wav->last_duration = 0;
70 73
     wav->minpts = INT64_MAX;
... ...
@@ -125,6 +188,20 @@ static int wav_write_trailer(AVFormatContext *s)
125 125
     return 0;
126 126
 }
127 127
 
128
+#define OFFSET(x) offsetof(WAVContext, x)
129
+#define ENC AV_OPT_FLAG_ENCODING_PARAM
130
+static const AVOption options[] = {
131
+    { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), FF_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
132
+    { NULL },
133
+};
134
+
135
+static const AVClass wav_muxer_class = {
136
+    .class_name = "WAV muxer",
137
+    .item_name  = av_default_item_name,
138
+    .option     = options,
139
+    .version    = LIBAVUTIL_VERSION_INT,
140
+};
141
+
128 142
 AVOutputFormat ff_wav_muxer = {
129 143
     "wav",
130 144
     NULL_IF_CONFIG_SMALL("WAV format"),
... ...
@@ -137,6 +214,7 @@ AVOutputFormat ff_wav_muxer = {
137 137
     wav_write_packet,
138 138
     wav_write_trailer,
139 139
     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
140
+    .priv_class = &wav_muxer_class,
140 141
 };
141 142
 #endif /* CONFIG_WAV_MUXER */
142 143