Mercurial > libavformat.hg
annotate avienc.c @ 94:79b7d70d6c37 libavformat
I420 patch by (Sebastien Bechet <s dot bechet at av7 dot net>)
author | michaelni |
---|---|
date | Wed, 26 Mar 2003 23:25:18 +0000 |
parents | 27d6df9208d4 |
children | c48108fe538e |
rev | line source |
---|---|
0 | 1 /* |
2 * AVI encoder. | |
3 * Copyright (c) 2000 Fabrice Bellard. | |
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 Lesser 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 #include "avformat.h" | |
20 #include "avi.h" | |
21 | |
22 /* | |
23 * TODO: | |
24 * - fill all fields if non streamed (nb_frames for example) | |
25 */ | |
26 | |
27 typedef struct AVIIndex { | |
28 unsigned char tag[4]; | |
29 unsigned int flags, pos, len; | |
30 struct AVIIndex *next; | |
31 } AVIIndex; | |
32 | |
33 typedef struct { | |
34 offset_t movi_list, frames_hdr_all, frames_hdr_strm[MAX_STREAMS]; | |
35 int audio_strm_length[MAX_STREAMS]; | |
36 AVIIndex *first, *last; | |
37 } AVIContext; | |
38 | |
39 offset_t start_tag(ByteIOContext *pb, const char *tag) | |
40 { | |
41 put_tag(pb, tag); | |
42 put_le32(pb, 0); | |
43 return url_ftell(pb); | |
44 } | |
45 | |
46 void end_tag(ByteIOContext *pb, offset_t start) | |
47 { | |
48 offset_t pos; | |
49 | |
50 pos = url_ftell(pb); | |
51 url_fseek(pb, start - 4, SEEK_SET); | |
65 | 52 put_le32(pb, (uint32_t)(pos - start)); |
0 | 53 url_fseek(pb, pos, SEEK_SET); |
54 } | |
55 | |
56 /* Note: when encoding, the first matching tag is used, so order is | |
57 important if multiple tags possible for a given codec. */ | |
58 const CodecTag codec_bmp_tags[] = { | |
59 { CODEC_ID_H263, MKTAG('H', '2', '6', '3') }, | |
60 { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') }, | |
61 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */ | |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
62 |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
63 /* added based on MPlayer */ |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
64 { CODEC_ID_H263I, MKTAG('i', '2', '6', '3') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
65 { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
66 { CODEC_ID_H263P, MKTAG('h', '2', '6', '3') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
67 { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
68 |
0 | 69 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') }, |
70 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 }, | |
71 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x'), .invalid_asf = 1 }, | |
72 { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 }, | |
73 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 }, | |
74 { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd'), .invalid_asf = 1 }, | |
75 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's'), .invalid_asf = 1 }, | |
76 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') }, | |
77 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') }, | |
78 { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') }, | |
79 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */ | |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
80 |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
81 /* added based on MPlayer */ |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
82 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
83 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', '1') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
84 { CODEC_ID_MPEG4, MKTAG('X', 'v', 'i', 'D') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
85 { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
86 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
87 { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
88 |
0 | 89 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */ |
90 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '3'), .invalid_asf = 1 }, | |
91 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') }, | |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
92 |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
93 /* added based on MPlayer */ |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
94 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
95 { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', 'g', '3') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
96 { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', '4', '3') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
97 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
98 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '5') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
99 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
100 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '6') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
101 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
102 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '4') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
103 { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
104 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
105 { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '1') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
106 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
107 { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '0') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
108 |
0 | 109 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') }, |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
110 |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
111 /* added based on MPlayer */ |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
112 { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
113 { CODEC_ID_MSMPEG4V2, MKTAG('d', 'i', 'v', '2') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
114 { CODEC_ID_MSMPEG4V2, MKTAG('m', 'p', '4', '2') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
115 |
0 | 116 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') }, |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
117 |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
118 /* added based on MPlayer */ |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
119 { CODEC_ID_MSMPEG4V1, MKTAG('D', 'I', 'V', '4') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
120 { CODEC_ID_MSMPEG4V1, MKTAG('d', 'i', 'v', '4') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
121 { CODEC_ID_MSMPEG4V1, MKTAG('m', 'p', 'g', '4') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
122 |
0 | 123 { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') }, |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
124 |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
125 /* added based on MPlayer */ |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
126 { CODEC_ID_WMV1, MKTAG('w', 'm', 'v', '1') }, |
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
89
diff
changeset
|
127 |
77 | 128 { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') }, |
0 | 129 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') }, |
130 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') }, | |
131 { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') }, | |
132 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') }, | |
133 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') }, | |
134 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') }, | |
135 { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') }, | |
136 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') }, | |
15 | 137 { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') }, |
138 { CODEC_ID_HUFFYUV, MKTAG('h', 'f', 'y', 'u') }, | |
61 | 139 { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') }, |
140 { CODEC_ID_CYUV, MKTAG('c', 'y', 'u', 'v') }, | |
89
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
141 { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') }, |
94
79b7d70d6c37
I420 patch by (Sebastien Bechet <s dot bechet at av7 dot net>)
michaelni
parents:
90
diff
changeset
|
142 { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') }, |
0 | 143 { 0, 0 }, |
144 }; | |
145 | |
146 unsigned int codec_get_tag(const CodecTag *tags, int id) | |
147 { | |
148 while (tags->id != 0) { | |
149 if (tags->id == id) | |
150 return tags->tag; | |
151 tags++; | |
152 } | |
153 return 0; | |
154 } | |
155 | |
156 static unsigned int codec_get_asf_tag(const CodecTag *tags, int id) | |
157 { | |
158 while (tags->id != 0) { | |
159 if (!tags->invalid_asf && tags->id == id) | |
160 return tags->tag; | |
161 tags++; | |
162 } | |
163 return 0; | |
164 } | |
165 | |
166 int codec_get_id(const CodecTag *tags, unsigned int tag) | |
167 { | |
168 while (tags->id != 0) { | |
169 if (tags->tag == tag) | |
170 return tags->id; | |
171 tags++; | |
172 } | |
173 return 0; | |
174 } | |
175 | |
176 unsigned int codec_get_bmp_tag(int id) | |
177 { | |
178 return codec_get_tag(codec_bmp_tags, id); | |
179 } | |
180 | |
181 /* BITMAPINFOHEADER header */ | |
182 void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf) | |
183 { | |
76 | 184 put_le32(pb, 40 + enc->extradata_size); /* size */ |
0 | 185 put_le32(pb, enc->width); |
186 put_le32(pb, enc->height); | |
187 put_le16(pb, 1); /* planes */ | |
76 | 188 |
189 put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */ | |
0 | 190 /* compression type */ |
191 put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : codec_get_tag(tags, enc->codec_id)); | |
192 put_le32(pb, enc->width * enc->height * 3); | |
193 put_le32(pb, 0); | |
194 put_le32(pb, 0); | |
195 put_le32(pb, 0); | |
196 put_le32(pb, 0); | |
76 | 197 |
198 put_buffer(pb, enc->extradata, enc->extradata_size); | |
199 | |
200 if (enc->extradata_size & 1) | |
201 put_byte(pb, 0); | |
0 | 202 } |
203 | |
204 static void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale) | |
205 { | |
206 switch(stream->codec_id) { | |
207 case CODEC_ID_PCM_S16LE: | |
208 *au_scale = *au_ssize = 2*stream->channels; | |
209 *au_byterate = *au_ssize * stream->sample_rate; | |
210 break; | |
211 case CODEC_ID_PCM_U8: | |
212 case CODEC_ID_PCM_ALAW: | |
213 case CODEC_ID_PCM_MULAW: | |
214 *au_scale = *au_ssize = stream->channels; | |
215 *au_byterate = *au_ssize * stream->sample_rate; | |
216 break; | |
217 case CODEC_ID_MP2: | |
218 *au_ssize = 1; | |
219 *au_scale = 1; | |
220 *au_byterate = stream->bit_rate / 8; | |
221 case CODEC_ID_MP3LAME: | |
222 *au_ssize = 1; | |
223 *au_scale = 1; | |
224 *au_byterate = stream->bit_rate / 8; | |
225 default: | |
226 *au_ssize = 1; | |
227 *au_scale = 1; | |
228 *au_byterate = stream->bit_rate / 8; | |
229 break; | |
230 } | |
231 } | |
232 | |
233 static int avi_write_header(AVFormatContext *s) | |
234 { | |
235 AVIContext *avi = s->priv_data; | |
236 ByteIOContext *pb = &s->pb; | |
237 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale; | |
238 AVCodecContext *stream, *video_enc; | |
239 offset_t list1, list2, strh, strf; | |
240 | |
241 put_tag(pb, "RIFF"); | |
242 put_le32(pb, 0); /* file length */ | |
243 put_tag(pb, "AVI "); | |
244 | |
245 /* header list */ | |
246 list1 = start_tag(pb, "LIST"); | |
247 put_tag(pb, "hdrl"); | |
248 | |
249 /* avi header */ | |
250 put_tag(pb, "avih"); | |
251 put_le32(pb, 14 * 4); | |
252 bitrate = 0; | |
253 | |
254 video_enc = NULL; | |
255 for(n=0;n<s->nb_streams;n++) { | |
256 stream = &s->streams[n]->codec; | |
257 bitrate += stream->bit_rate; | |
258 if (stream->codec_type == CODEC_TYPE_VIDEO) | |
259 video_enc = stream; | |
260 } | |
261 | |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
262 /* allowing audio-only AVI file |
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
263 |
0 | 264 if (!video_enc) { |
265 av_free(avi); | |
266 return -1; | |
267 } | |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
268 */ |
0 | 269 nb_frames = 0; |
270 | |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
271 if(video_enc){ |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
77
diff
changeset
|
272 put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->frame_rate_base / video_enc->frame_rate)); |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
273 } else { |
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
274 put_le32(pb, 0); |
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
275 } |
0 | 276 put_le32(pb, bitrate / 8); /* XXX: not quite exact */ |
277 put_le32(pb, 0); /* padding */ | |
278 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */ | |
279 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */ | |
280 put_le32(pb, nb_frames); /* nb frames, filled later */ | |
281 put_le32(pb, 0); /* initial frame */ | |
282 put_le32(pb, s->nb_streams); /* nb streams */ | |
283 put_le32(pb, 1024 * 1024); /* suggested buffer size */ | |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
284 if(video_enc){ |
0 | 285 put_le32(pb, video_enc->width); |
286 put_le32(pb, video_enc->height); | |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
287 } else { |
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
288 put_le32(pb, 0); |
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
289 put_le32(pb, 0); |
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
290 } |
0 | 291 put_le32(pb, 0); /* reserved */ |
292 put_le32(pb, 0); /* reserved */ | |
293 put_le32(pb, 0); /* reserved */ | |
294 put_le32(pb, 0); /* reserved */ | |
295 | |
296 /* stream list */ | |
297 for(i=0;i<n;i++) { | |
298 list2 = start_tag(pb, "LIST"); | |
299 put_tag(pb, "strl"); | |
300 | |
301 stream = &s->streams[i]->codec; | |
302 | |
89
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
303 /* FourCC should really be set by the codec itself */ |
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
304 if (! stream->codec_tag) { |
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
305 stream->codec_tag = codec_get_bmp_tag(stream->codec_id); |
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
306 } |
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
307 |
0 | 308 /* stream generic header */ |
309 strh = start_tag(pb, "strh"); | |
310 switch(stream->codec_type) { | |
311 case CODEC_TYPE_VIDEO: | |
312 put_tag(pb, "vids"); | |
89
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
313 put_le32(pb, stream->codec_tag); |
0 | 314 put_le32(pb, 0); /* flags */ |
315 put_le16(pb, 0); /* priority */ | |
316 put_le16(pb, 0); /* language */ | |
317 put_le32(pb, 0); /* initial frame */ | |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
65
diff
changeset
|
318 |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
77
diff
changeset
|
319 put_le32(pb, stream->frame_rate_base); /* scale */ |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
77
diff
changeset
|
320 put_le32(pb, stream->frame_rate); /* rate */ |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
65
diff
changeset
|
321 |
0 | 322 put_le32(pb, 0); /* start */ |
323 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */ | |
324 put_le32(pb, nb_frames); /* length, XXX: fill later */ | |
325 put_le32(pb, 1024 * 1024); /* suggested buffer size */ | |
326 put_le32(pb, -1); /* quality */ | |
327 put_le32(pb, stream->width * stream->height * 3); /* sample size */ | |
328 put_le16(pb, 0); | |
329 put_le16(pb, 0); | |
330 put_le16(pb, stream->width); | |
331 put_le16(pb, stream->height); | |
332 break; | |
333 case CODEC_TYPE_AUDIO: | |
334 put_tag(pb, "auds"); | |
335 put_le32(pb, 1); /* tag */ | |
336 put_le32(pb, 0); /* flags */ | |
337 put_le16(pb, 0); /* priority */ | |
338 put_le16(pb, 0); /* language */ | |
339 put_le32(pb, 0); /* initial frame */ | |
340 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale); | |
341 put_le32(pb, au_scale); /* scale */ | |
342 put_le32(pb, au_byterate); /* rate */ | |
343 put_le32(pb, 0); /* start */ | |
344 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */ | |
345 put_le32(pb, 0); /* length, XXX: filled later */ | |
346 put_le32(pb, 12 * 1024); /* suggested buffer size */ | |
347 put_le32(pb, -1); /* quality */ | |
348 put_le32(pb, au_ssize); /* sample size */ | |
349 put_le32(pb, 0); | |
350 put_le32(pb, 0); | |
351 break; | |
352 default: | |
353 av_abort(); | |
354 } | |
355 end_tag(pb, strh); | |
356 | |
357 strf = start_tag(pb, "strf"); | |
358 switch(stream->codec_type) { | |
359 case CODEC_TYPE_VIDEO: | |
360 put_bmp_header(pb, stream, codec_bmp_tags, 0); | |
361 break; | |
362 case CODEC_TYPE_AUDIO: | |
363 if (put_wav_header(pb, stream) < 0) { | |
364 av_free(avi); | |
365 return -1; | |
366 } | |
367 break; | |
368 default: | |
369 av_abort(); | |
370 } | |
371 end_tag(pb, strf); | |
372 end_tag(pb, list2); | |
373 } | |
374 | |
375 end_tag(pb, list1); | |
376 | |
377 avi->movi_list = start_tag(pb, "LIST"); | |
378 avi->first = NULL; | |
379 avi->last = NULL; | |
380 put_tag(pb, "movi"); | |
381 | |
382 put_flush_packet(pb); | |
383 | |
384 return 0; | |
385 } | |
386 | |
387 static int avi_write_packet(AVFormatContext *s, int stream_index, | |
65 | 388 uint8_t *buf, int size, int force_pts) |
0 | 389 { |
390 AVIContext *avi = s->priv_data; | |
391 ByteIOContext *pb = &s->pb; | |
392 AVIIndex *idx; | |
393 unsigned char tag[5]; | |
394 unsigned int flags; | |
395 AVCodecContext *enc; | |
396 | |
397 enc = &s->streams[stream_index]->codec; | |
398 | |
399 tag[0] = '0'; | |
400 tag[1] = '0' + stream_index; | |
401 if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
402 tag[2] = 'd'; | |
403 tag[3] = 'c'; | |
7 | 404 flags = enc->coded_frame->key_frame ? 0x10 : 0x00; |
0 | 405 } else { |
406 tag[2] = 'w'; | |
407 tag[3] = 'b'; | |
408 flags = 0x10; | |
409 } | |
410 if (enc->codec_type == CODEC_TYPE_AUDIO) | |
411 avi->audio_strm_length[stream_index] += size; | |
412 | |
413 if (!url_is_streamed(&s->pb)) { | |
414 idx = av_malloc(sizeof(AVIIndex)); | |
415 memcpy(idx->tag, tag, 4); | |
416 idx->flags = flags; | |
417 idx->pos = url_ftell(pb) - avi->movi_list; | |
418 idx->len = size; | |
419 idx->next = NULL; | |
420 if (!avi->last) | |
421 avi->first = idx; | |
422 else | |
423 avi->last->next = idx; | |
424 avi->last = idx; | |
425 } | |
426 | |
427 put_buffer(pb, tag, 4); | |
428 put_le32(pb, size); | |
429 put_buffer(pb, buf, size); | |
430 if (size & 1) | |
431 put_byte(pb, 0); | |
432 | |
433 put_flush_packet(pb); | |
434 return 0; | |
435 } | |
436 | |
437 static int avi_write_trailer(AVFormatContext *s) | |
438 { | |
439 ByteIOContext *pb = &s->pb; | |
440 AVIContext *avi = s->priv_data; | |
441 offset_t file_size, idx_chunk; | |
442 int n, nb_frames, au_byterate, au_ssize, au_scale; | |
443 AVCodecContext *stream; | |
444 AVIIndex *idx; | |
445 | |
446 if (!url_is_streamed(&s->pb)) { | |
447 end_tag(pb, avi->movi_list); | |
448 | |
449 idx_chunk = start_tag(pb, "idx1"); | |
450 idx = avi->first; | |
451 while (idx != NULL) { | |
452 put_buffer(pb, idx->tag, 4); | |
453 put_le32(pb, idx->flags); | |
454 put_le32(pb, idx->pos); | |
455 put_le32(pb, idx->len); | |
456 idx = idx->next; | |
457 } | |
458 end_tag(pb, idx_chunk); | |
459 | |
460 /* update file size */ | |
461 file_size = url_ftell(pb); | |
462 url_fseek(pb, 4, SEEK_SET); | |
65 | 463 put_le32(pb, (uint32_t)(file_size - 8)); |
0 | 464 |
465 /* Fill in frame/sample counters */ | |
466 nb_frames = 0; | |
467 for(n=0;n<s->nb_streams;n++) { | |
468 if (avi->frames_hdr_strm[n] != 0) { | |
469 stream = &s->streams[n]->codec; | |
470 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET); | |
471 if (stream->codec_type == CODEC_TYPE_VIDEO) { | |
472 put_le32(pb, stream->frame_number); | |
473 if (nb_frames < stream->frame_number) | |
474 nb_frames = stream->frame_number; | |
475 } else { | |
476 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) { | |
477 put_le32(pb, stream->frame_number); | |
478 nb_frames += stream->frame_number; | |
479 } else { | |
480 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale); | |
481 put_le32(pb, avi->audio_strm_length[n] / au_ssize); | |
482 } | |
483 } | |
484 } | |
485 } | |
486 if (avi->frames_hdr_all != 0) { | |
487 url_fseek(pb, avi->frames_hdr_all, SEEK_SET); | |
488 put_le32(pb, nb_frames); | |
489 } | |
490 url_fseek(pb, file_size, SEEK_SET); | |
491 } | |
492 put_flush_packet(pb); | |
493 return 0; | |
494 } | |
495 | |
496 static AVOutputFormat avi_oformat = { | |
497 "avi", | |
498 "avi format", | |
499 "video/x-msvideo", | |
500 "avi", | |
501 sizeof(AVIContext), | |
502 CODEC_ID_MP2, | |
503 CODEC_ID_MSMPEG4V3, | |
504 avi_write_header, | |
505 avi_write_packet, | |
506 avi_write_trailer, | |
507 }; | |
508 | |
509 int avienc_init(void) | |
510 { | |
511 av_register_output_format(&avi_oformat); | |
512 return 0; | |
513 } |