annotate matroskaenc.c @ 2446:b2f9523ee424 libavformat

Make sure to return a value in functions that return a value
author conrad
date Wed, 05 Sep 2007 00:23:17 +0000
parents 86466f60fc5d
children 54f0053730cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
1 /*
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
2 * Matroska file muxer
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
3 * Copyright (c) 2007 David Conrad
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
4 *
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
5 * This file is part of FFmpeg.
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
6 *
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
11 *
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
15 * Lesser General Public License for more details.
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
16 *
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
20 */
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
21
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
22 #include "avformat.h"
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
23 #include "riff.h"
2435
4dde24899d01 Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents: 2434
diff changeset
24 #include "xiph.h"
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
25 #include "matroska.h"
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
26
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
27 typedef struct MatroskaMuxContext {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
28 offset_t segment;
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
29 offset_t cluster;
2437
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
30 uint64_t cluster_pts;
2443
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
31 offset_t duration_offset;
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
32 uint64_t duration;
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
33 } MatroskaMuxContext;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
34
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
35 static void put_ebml_id(ByteIOContext *pb, unsigned int id)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
36 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
37 if (id >= 0x3fffff)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
38 put_byte(pb, id >> 24);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
39 if (id >= 0x7fff)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
40 put_byte(pb, id >> 16);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
41 if (id >= 0xff)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
42 put_byte(pb, id >> 8);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
43 put_byte(pb, id);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
44 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
45
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
46 // XXX: test this thoroughly and get rid of minbytes hack (currently needed to
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
47 // use up all of the space reserved in start_ebml_master)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
48 static void put_ebml_size(ByteIOContext *pb, uint64_t size, int minbytes)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
49 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
50 int bytes = minbytes;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
51
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
52 // sizes larger than this are currently undefined in EBML
2440
69e2592531b6 Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents: 2439
diff changeset
53 // so write "unknown" size
69e2592531b6 Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents: 2439
diff changeset
54 size = FFMIN(size, (1ULL<<56)-1);
69e2592531b6 Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents: 2439
diff changeset
55
69e2592531b6 Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents: 2439
diff changeset
56 while (size >> (bytes*7 + 7)) bytes++;
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
57
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
58 put_byte(pb, (0x80 >> bytes) | (size >> bytes*8));
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
59 for (bytes -= 1; bytes >= 0; bytes--)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
60 put_byte(pb, size >> bytes*8);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
61 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
62
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
63 static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
64 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
65 int bytes = 1;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
66 while (val >> bytes*8) bytes++;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
67
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
68 put_ebml_id(pb, elementid);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
69 put_ebml_size(pb, bytes, 0);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
70 for (bytes -= 1; bytes >= 0; bytes--)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
71 put_byte(pb, val >> bytes*8);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
72 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
73
2430
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
74 static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val)
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
75 {
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
76 // XXX: single-precision floats?
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
77 put_ebml_id(pb, elementid);
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
78 put_ebml_size(pb, 8, 0);
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
79 put_be64(pb, av_dbl2int(val));
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
80 }
da0227bd105a put_ebml_float()
conrad
parents: 2429
diff changeset
81
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
82 static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
2429
326e6fcf9f85 Const correctness
conrad
parents: 2428
diff changeset
83 const uint8_t *buf, int size)
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
84 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
85 put_ebml_id(pb, elementid);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
86 put_ebml_size(pb, size, 0);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
87 put_buffer(pb, buf, size);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
88 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
89
2429
326e6fcf9f85 Const correctness
conrad
parents: 2428
diff changeset
90 static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str)
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
91 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
92 put_ebml_binary(pb, elementid, str, strlen(str));
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
93 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
94
2442
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
95 // this reserves exactly the amount of space specified by size, which must be at least 2
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
96 static void put_ebml_void(ByteIOContext *pb, uint64_t size)
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
97 {
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
98 offset_t currentpos = url_ftell(pb);
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
99
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
100 if (size < 2)
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
101 return;
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
102
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
103 put_ebml_id(pb, EBML_ID_VOID);
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
104 // we need to subtract the length needed to store the size from the size we need to reserve
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
105 // so 2 cases, we use 8 bytes to store the size if possible, 1 byte otherwise
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
106 if (size < 10)
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
107 put_ebml_size(pb, size-1, 0);
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
108 else
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
109 put_ebml_size(pb, size-9, 7);
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
110 url_fseek(pb, currentpos + size, SEEK_SET);
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
111 }
806e9837fea4 put_ebml_void()
conrad
parents: 2441
diff changeset
112
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
113 static offset_t start_ebml_master(ByteIOContext *pb, unsigned int elementid)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
114 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
115 put_ebml_id(pb, elementid);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
116 // XXX: this always reserves the maximum needed space to store any size value
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
117 // we should be smarter (additional parameter for expected size?)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
118 put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
119 return url_ftell(pb);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
120 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
121
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
122 static void end_ebml_master(ByteIOContext *pb, offset_t start)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
123 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
124 offset_t pos = url_ftell(pb);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
125
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
126 url_fseek(pb, start - 8, SEEK_SET);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
127 put_ebml_size(pb, pos - start, 7);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
128 url_fseek(pb, pos, SEEK_SET);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
129 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
130
2444
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
131 static int put_xiph_codecpriv(ByteIOContext *pb, AVCodecContext *codec)
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
132 {
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
133 offset_t codecprivate;
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
134 uint8_t *header_start[3];
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
135 int header_len[3];
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
136 int first_header_size;
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
137 int j, k;
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
138
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
139 if (codec->codec_id == CODEC_ID_VORBIS)
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
140 first_header_size = 30;
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
141 else
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
142 first_header_size = 42;
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
143
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
144 if (ff_split_xiph_headers(codec->extradata, codec->extradata_size,
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
145 first_header_size, header_start, header_len) < 0) {
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
146 av_log(codec, AV_LOG_ERROR, "Extradata corrupt.\n");
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
147 return -1;
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
148 }
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
149
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
150 codecprivate = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE);
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
151 put_byte(pb, 2); // number packets - 1
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
152 for (j = 0; j < 2; j++) {
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
153 for (k = 0; k < header_len[j] / 255; k++)
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
154 put_byte(pb, 255);
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
155 put_byte(pb, header_len[j] % 255);
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
156 }
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
157 for (j = 0; j < 3; j++)
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
158 put_buffer(pb, header_start[j], header_len[j]);
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
159 end_ebml_master(pb, codecprivate);
2446
b2f9523ee424 Make sure to return a value in functions that return a value
conrad
parents: 2445
diff changeset
160
b2f9523ee424 Make sure to return a value in functions that return a value
conrad
parents: 2445
diff changeset
161 return 0;
2444
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
162 }
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
163
2445
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
164 static int mkv_write_tracks(AVFormatContext *s)
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
165 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
166 MatroskaMuxContext *mkv = s->priv_data;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
167 ByteIOContext *pb = &s->pb;
2445
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
168 offset_t tracks;
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
169 int i, j;
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
170
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
171 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
172 for (i = 0; i < s->nb_streams; i++) {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
173 AVStream *st = s->streams[i];
2432
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
174 AVCodecContext *codec = st->codec;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
175 offset_t subinfo, track;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
176 int native_id = 0;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
177
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
178 track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY);
2439
04ae6910d903 Track number and UID only have to be nonzero
conrad
parents: 2438
diff changeset
179 put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1);
04ae6910d903 Track number and UID only have to be nonzero
conrad
parents: 2438
diff changeset
180 put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
2432
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
181 put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
182
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
183 if (st->language[0])
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
184 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, st->language);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
185
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
186 // look for a codec id string specific to mkv to use, if none are found, use AVI codes
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
187 for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) {
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
188 if (ff_mkv_codec_tags[j].id == codec->codec_id) {
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
189 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
190 native_id = 1;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
191 break;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
192 }
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
193 }
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
194
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
195 // XXX: CodecPrivate for vorbis, theora, aac, native mpeg4, ...
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
196 if (native_id) {
2435
4dde24899d01 Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents: 2434
diff changeset
197 if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA) {
2444
d954330691c5 Move Xiph's CodecPrivate writing code to its own function
conrad
parents: 2443
diff changeset
198 if (put_xiph_codecpriv(pb, codec) < 0)
2435
4dde24899d01 Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents: 2434
diff changeset
199 return -1;
4dde24899d01 Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents: 2434
diff changeset
200 } else {
4dde24899d01 Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents: 2434
diff changeset
201 put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codec->extradata, codec->extradata_size);
4dde24899d01 Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents: 2434
diff changeset
202 }
2432
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
203 }
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
204
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
205 switch (codec->codec_type) {
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
206 case CODEC_TYPE_VIDEO:
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
207 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
208
2432
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
209 if (!native_id) {
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
210 offset_t bmp_header;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
211 // if there is no mkv-specific codec id, use VFW mode
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
212 if (!codec->codec_tag)
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
213 codec->codec_tag = codec_get_tag(codec_bmp_tags, codec->codec_id);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
214
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
215 put_ebml_string(pb, MATROSKA_ID_CODECID, MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
216 // XXX: codec private isn't a master; is there a better way to re-use put_bmp_header?
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
217 bmp_header = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
218 put_bmp_header(pb, codec, codec_bmp_tags, 0);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
219 end_ebml_master(pb, bmp_header);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
220 }
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
221 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
222 // XXX: interlace flag?
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
223 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
224 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
225 // XXX: display width/height
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
226 end_ebml_master(pb, subinfo);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
227 break;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
228
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
229 case CODEC_TYPE_AUDIO:
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
230 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
231
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
232 // XXX: A_MS/ACM
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
233 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
234 put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
235 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, codec->sample_rate);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
236 // XXX: output sample freq (for sbr) and bitdepth (for pcm)
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
237 end_ebml_master(pb, subinfo);
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
238 break;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
239
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
240 default:
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
241 av_log(s, AV_LOG_ERROR, "Only audio and video are supported for Matroska.");
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
242 break;
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
243 }
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
244 end_ebml_master(pb, track);
2432
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
245
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
246 // ms precision is the de-facto standard timescale for mkv files
68743e7fa627 First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents: 2431
diff changeset
247 av_set_pts_info(st, 64, 1, 1000);
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
248 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
249 end_ebml_master(pb, tracks);
2446
b2f9523ee424 Make sure to return a value in functions that return a value
conrad
parents: 2445
diff changeset
250 return 0;
2445
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
251 }
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
252
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
253 static int mkv_write_header(AVFormatContext *s)
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
254 {
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
255 MatroskaMuxContext *mkv = s->priv_data;
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
256 ByteIOContext *pb = &s->pb;
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
257 offset_t ebml_header, segment_info;
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
258
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
259 ebml_header = start_ebml_master(pb, EBML_ID_HEADER);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
260 put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
261 put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
262 put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
263 put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
264 put_ebml_string (pb, EBML_ID_DOCTYPE , "matroska");
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
265 put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
266 put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
267 end_ebml_master(pb, ebml_header);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
268
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
269 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
270
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
271 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
272 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
273 if (strlen(s->title))
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
274 put_ebml_string(pb, MATROSKA_ID_TITLE, s->title);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
275 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
276 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP, LIBAVFORMAT_IDENT);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
277 // XXX: both are required; something better for writing app?
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
278 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
279 }
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
280 // XXX: segment UID
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
281 // reserve space for the duration
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
282 mkv->duration = 0;
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
283 mkv->duration_offset = url_ftell(pb);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
284 put_ebml_void(pb, 11); // assumes double-precision float to be written
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
285 end_ebml_master(pb, segment_info);
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
286
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
287 if (mkv_write_tracks(s) < 0)
86466f60fc5d Move writing the tracks element to its own function
conrad
parents: 2444
diff changeset
288 return -1;
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
289
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
290 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER);
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
291 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, 0);
2437
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
292 mkv->cluster_pts = 0;
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
293
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
294 return 0;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
295 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
296
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
297 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
298 {
2437
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
299 MatroskaMuxContext *mkv = s->priv_data;
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
300 ByteIOContext *pb = &s->pb;
2434
273c76713989 Compile fix; missed a variable declaration in the last commit
conrad
parents: 2433
diff changeset
301 offset_t block;
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
302
2437
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
303 // start a new cluster every 5 MB or 5 sec
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
304 if (url_ftell(pb) > mkv->cluster + 5*1024*1024 || pkt->pts > mkv->cluster_pts + 5000) {
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
305 end_ebml_master(pb, mkv->cluster);
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
306 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER);
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
307 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, pkt->pts);
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
308 mkv->cluster_pts = pkt->pts;
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
309 }
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
310
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
311 block = start_ebml_master(pb, MATROSKA_ID_SIMPLEBLOCK);
2439
04ae6910d903 Track number and UID only have to be nonzero
conrad
parents: 2438
diff changeset
312 put_byte(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
2437
36350b4c94fa Start a new cluster every 5 MB or 5 seconds
conrad
parents: 2436
diff changeset
313 put_be16(pb, pkt->pts - mkv->cluster_pts);
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
314 put_byte(pb, !!(pkt->flags & PKT_FLAG_KEY));
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
315 put_buffer(pb, pkt->data, pkt->size);
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
316 end_ebml_master(pb, block);
2443
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
317
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
318 mkv->duration = pkt->pts + pkt->duration;
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
319 return 0;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
320 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
321
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
322 static int mkv_write_trailer(AVFormatContext *s)
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
323 {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
324 MatroskaMuxContext *mkv = s->priv_data;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
325 ByteIOContext *pb = &s->pb;
2443
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
326 offset_t currentpos;
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
327
2433
0c047310f205 Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents: 2432
diff changeset
328 end_ebml_master(pb, mkv->cluster);
2443
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
329
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
330 // update the duration
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
331 currentpos = url_ftell(pb);
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
332 url_fseek(pb, mkv->duration_offset, SEEK_SET);
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
333 put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
334 url_fseek(pb, currentpos, SEEK_SET);
7cb4e029fd09 Write the duration of the file
conrad
parents: 2442
diff changeset
335
2425
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
336 end_ebml_master(pb, mkv->segment);
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
337 return 0;
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
338 }
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
339
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
340 AVOutputFormat matroska_muxer = {
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
341 "matroska",
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
342 "Matroska File Format",
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
343 "video/x-matroska",
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
344 "mkv",
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
345 sizeof(MatroskaMuxContext),
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
346 CODEC_ID_MP2,
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
347 CODEC_ID_MPEG4,
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
348 mkv_write_header,
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
349 mkv_write_packet,
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
350 mkv_write_trailer,
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
351 .codec_tag = (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},
8c51e92edd7d Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff changeset
352 };