Mercurial > libavformat.hg
annotate gxfenc.c @ 4790:0493d65debd7 libavformat
set wrong_dts for iMovie created files which has huge ctts delay, fix ffmpeg_sample.m4v
author | bcoudurier |
---|---|
date | Wed, 25 Mar 2009 06:07:13 +0000 |
parents | c12f591bf750 |
children | 3be97dd76d2c |
rev | line source |
---|---|
1183 | 1 /* |
2 * GXF muxer. | |
4251
77e0c7511d41
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
4098
diff
changeset
|
3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com> |
1183 | 4 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1322
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1322
diff
changeset
|
6 * |
2252
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public |
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
9 * License as published by the Free Software Foundation; either |
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
1183 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1322
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
1183 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
2252
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
15 * Lesser General Public License for more details. |
1183 | 16 * |
2252
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
708e6e93d6f2
Smartjog granted permission to relicense as LGPL 2.1
bcoudurier
parents:
2251
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1322
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1183 | 20 */ |
21 | |
3286 | 22 #include "libavutil/fifo.h" |
1183 | 23 #include "avformat.h" |
24 #include "gxf.h" | |
25 #include "riff.h" | |
4401 | 26 #include "audiointerleave.h" |
1183 | 27 |
28 #define GXF_AUDIO_PACKET_SIZE 65536 | |
29 | |
30 typedef struct GXFStreamContext { | |
4401 | 31 AudioInterleaveContext aic; |
1183 | 32 uint32_t track_type; |
33 uint32_t sample_size; | |
34 uint32_t sample_rate; | |
35 uint16_t media_type; | |
36 uint16_t media_info; | |
37 int frame_rate_index; | |
38 int lines_index; | |
39 int fields; | |
40 int iframes; | |
41 int pframes; | |
42 int bframes; | |
43 int p_per_gop; | |
4456
833d30447d64
rename wrongly named b_per_gop to b_per_i_or_p according to specs
bcoudurier
parents:
4426
diff
changeset
|
44 int b_per_i_or_p; ///< number of B frames per I frame or P frame |
1261 | 45 int first_gop_closed; |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
46 unsigned order; ///< interleaving order |
1183 | 47 } GXFStreamContext; |
48 | |
49 typedef struct GXFContext { | |
4479 | 50 uint32_t nb_fields; |
1183 | 51 uint16_t audio_tracks; |
52 uint16_t mpeg_tracks; | |
53 int64_t creation_time; | |
54 uint32_t umf_start_offset; | |
55 uint32_t umf_track_offset; | |
56 uint32_t umf_media_offset; | |
57 uint32_t umf_length; | |
58 uint16_t umf_track_size; | |
59 uint16_t umf_media_size; | |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
60 AVRational time_base; |
1183 | 61 int flags; |
4676 | 62 GXFStreamContext timecode_track; |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
63 unsigned *flt_entries; ///< offsets of packets /1024, starts after 2nd video field |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
64 unsigned flt_entries_nb; |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
65 uint64_t *map_offsets; ///< offset of map packets |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
66 unsigned map_offsets_nb; |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
67 unsigned packet_count; |
1183 | 68 } GXFContext; |
69 | |
4680 | 70 static const struct { |
71 int height, index; | |
72 } gxf_lines_tab[] = { | |
1183 | 73 { 480, 1 }, /* NTSC */ |
74 { 512, 1 }, /* NTSC + VBI */ | |
75 { 576, 2 }, /* PAL */ | |
76 { 608, 2 }, /* PAL + VBI */ | |
77 { 1080, 4 }, | |
78 { 720, 6 }, | |
79 }; | |
80 | |
1677
2a85c82b8538
add codec_id <-> codec_tag tables to AVIn/OutputFormat
michael
parents:
1542
diff
changeset
|
81 static const AVCodecTag gxf_media_types[] = { |
1183 | 82 { CODEC_ID_MJPEG , 3 }, /* NTSC */ |
83 { CODEC_ID_MJPEG , 4 }, /* PAL */ | |
84 { CODEC_ID_PCM_S24LE , 9 }, | |
85 { CODEC_ID_PCM_S16LE , 10 }, | |
86 { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */ | |
87 { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */ | |
88 { CODEC_ID_DVVIDEO , 13 }, /* NTSC */ | |
89 { CODEC_ID_DVVIDEO , 14 }, /* PAL */ | |
90 { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */ | |
91 { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */ | |
92 { CODEC_ID_AC3 , 17 }, | |
93 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */ | |
94 { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */ | |
95 { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */ | |
96 { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */ | |
97 { 0, 0 }, | |
98 }; | |
99 | |
4681
83c634df5c79
change path pattern to be similar to existing files, and remove useless buffer
bcoudurier
parents:
4680
diff
changeset
|
100 #define SERVER_PATH "EXT:/PDR/default/" |
83c634df5c79
change path pattern to be similar to existing files, and remove useless buffer
bcoudurier
parents:
4680
diff
changeset
|
101 #define ES_NAME_PATTERN "EXT:/PDR/default/ES." |
1183 | 102 |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
103 static int gxf_find_lines_index(AVStream *st) |
1183 | 104 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
105 GXFStreamContext *sc = st->priv_data; |
1183 | 106 int i; |
107 | |
108 for (i = 0; i < 6; ++i) { | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
109 if (st->codec->height == gxf_lines_tab[i].height) { |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
110 sc->lines_index = gxf_lines_tab[i].index; |
1183 | 111 return 0; |
112 } | |
113 } | |
114 return -1; | |
115 } | |
116 | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
117 static void gxf_write_padding(ByteIOContext *pb, int64_t to_pad) |
1183 | 118 { |
1213 | 119 for (; to_pad > 0; to_pad--) { |
1183 | 120 put_byte(pb, 0); |
121 } | |
122 } | |
123 | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
124 static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos) |
1183 | 125 { |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
126 int64_t curpos; |
1183 | 127 int size; |
128 | |
129 size = url_ftell(pb) - pos; | |
130 if (size % 4) { | |
131 gxf_write_padding(pb, 4 - size % 4); | |
132 size = url_ftell(pb) - pos; | |
133 } | |
134 curpos = url_ftell(pb); | |
135 url_fseek(pb, pos + 6, SEEK_SET); | |
136 put_be32(pb, size); | |
137 url_fseek(pb, curpos, SEEK_SET); | |
138 return curpos - pos; | |
139 } | |
140 | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
141 static int64_t updateSize(ByteIOContext *pb, int64_t pos) |
1183 | 142 { |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
143 int64_t curpos; |
1183 | 144 |
145 curpos = url_ftell(pb); | |
146 url_fseek(pb, pos, SEEK_SET); | |
147 put_be16(pb, curpos - pos - 2); | |
148 url_fseek(pb, curpos, SEEK_SET); | |
149 return curpos - pos; | |
150 } | |
151 | |
4098 | 152 static void gxf_write_packet_header(ByteIOContext *pb, GXFPktType type) |
1183 | 153 { |
154 put_be32(pb, 0); /* packet leader for synchro */ | |
155 put_byte(pb, 1); | |
156 put_byte(pb, type); /* map packet */ | |
157 put_be32(pb, 0); /* size */ | |
158 put_be32(pb, 0); /* reserved */ | |
159 put_byte(pb, 0xE1); /* trailer 1 */ | |
160 put_byte(pb, 0xE2); /* trailer 2 */ | |
161 } | |
162 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
163 static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, AVStream *st) |
1183 | 164 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
165 GXFStreamContext *sc = st->priv_data; |
1183 | 166 char buffer[1024]; |
4261 | 167 int size, starting_line; |
1183 | 168 |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
169 if (sc->iframes) { |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
170 sc->p_per_gop = sc->pframes / sc->iframes; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
171 if (sc->pframes % sc->iframes) |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
172 sc->p_per_gop++; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
173 if (sc->pframes) { |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
174 sc->b_per_i_or_p = sc->bframes / sc->pframes; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
175 if (sc->bframes % sc->pframes) |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
176 sc->b_per_i_or_p++; |
4457
6ed62b8257dd
compute b_per_i_or_p value correctly, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4456
diff
changeset
|
177 } |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
178 if (sc->p_per_gop > 9) |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
179 sc->p_per_gop = 9; /* ensure value won't take more than one char */ |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
180 if (sc->b_per_i_or_p > 9) |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
181 sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */ |
1183 | 182 } |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
183 if (st->codec->height == 512 || st->codec->height == 608) |
4261 | 184 starting_line = 7; // VBI |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
185 else if (st->codec->height == 480) |
4261 | 186 starting_line = 20; |
187 else | |
188 starting_line = 23; // default PAL | |
189 | |
1183 | 190 size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n" |
4261 | 191 "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n", |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
192 (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p, |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
193 st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1, |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
194 starting_line, st->codec->height / 16); |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
195 put_byte(pb, TRACK_MPG_AUX); |
1183 | 196 put_byte(pb, size + 1); |
197 put_buffer(pb, (uint8_t *)buffer, size + 1); | |
198 return size + 3; | |
199 } | |
200 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
201 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *sc) |
1183 | 202 { |
203 put_byte(pb, 0); /* fields */ | |
204 put_byte(pb, 0); /* seconds */ | |
205 put_byte(pb, 0); /* minutes */ | |
206 put_byte(pb, 0); /* flags + hours */ | |
207 /* reserved */ | |
208 put_be32(pb, 0); | |
209 return 8; | |
210 } | |
211 | |
4676 | 212 static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index) |
1183 | 213 { |
4676 | 214 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
215 int64_t pos; |
4676 | 216 int mpeg = sc->track_type == 4 || sc->track_type == 9; |
1183 | 217 |
218 /* track description section */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
219 put_byte(pb, sc->media_type + 0x80); |
4676 | 220 put_byte(pb, index + 0xC0); |
1183 | 221 |
222 pos = url_ftell(pb); | |
223 put_be16(pb, 0); /* size */ | |
224 | |
225 /* media file name */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
226 put_byte(pb, TRACK_NAME); |
1183 | 227 put_byte(pb, strlen(ES_NAME_PATTERN) + 3); |
228 put_tag(pb, ES_NAME_PATTERN); | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
229 put_be16(pb, sc->media_info); |
1183 | 230 put_byte(pb, 0); |
231 | |
4676 | 232 if (!mpeg) { |
1183 | 233 /* auxiliary information */ |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
234 put_byte(pb, TRACK_AUX); |
1183 | 235 put_byte(pb, 8); |
4676 | 236 if (sc->track_type == 3) |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
237 gxf_write_timecode_auxiliary(pb, sc); |
1183 | 238 else |
239 put_le64(pb, 0); | |
240 } | |
241 | |
242 /* file system version */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
243 put_byte(pb, TRACK_VER); |
1183 | 244 put_byte(pb, 4); |
245 put_be32(pb, 0); | |
246 | |
4676 | 247 if (mpeg) |
248 gxf_write_mpeg_auxiliary(pb, s->streams[index]); | |
1183 | 249 |
250 /* frame rate */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
251 put_byte(pb, TRACK_FPS); |
1183 | 252 put_byte(pb, 4); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
253 put_be32(pb, sc->frame_rate_index); |
1183 | 254 |
255 /* lines per frame */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
256 put_byte(pb, TRACK_LINES); |
1183 | 257 put_byte(pb, 4); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
258 put_be32(pb, sc->lines_index); |
1183 | 259 |
260 /* fields per frame */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
261 put_byte(pb, TRACK_FPF); |
1183 | 262 put_byte(pb, 4); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
263 put_be32(pb, sc->fields); |
1183 | 264 |
265 return updateSize(pb, pos); | |
266 } | |
267 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
268 static int gxf_write_material_data_section(AVFormatContext *s) |
1183 | 269 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
270 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
271 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
272 int64_t pos; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
273 const char *filename = strrchr(s->filename, '/'); |
1183 | 274 |
275 pos = url_ftell(pb); | |
276 put_be16(pb, 0); /* size */ | |
277 | |
278 /* name */ | |
279 if (filename) | |
280 filename++; | |
281 else | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
282 filename = s->filename; |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
283 put_byte(pb, MAT_NAME); |
1183 | 284 put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1); |
285 put_tag(pb, SERVER_PATH); | |
286 put_tag(pb, filename); | |
287 put_byte(pb, 0); | |
288 | |
289 /* first field */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
290 put_byte(pb, MAT_FIRST_FIELD); |
1183 | 291 put_byte(pb, 4); |
292 put_be32(pb, 0); | |
293 | |
294 /* last field */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
295 put_byte(pb, MAT_LAST_FIELD); |
1183 | 296 put_byte(pb, 4); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
297 put_be32(pb, gxf->nb_fields); |
1183 | 298 |
299 /* reserved */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
300 put_byte(pb, MAT_MARK_IN); |
1183 | 301 put_byte(pb, 4); |
302 put_be32(pb, 0); | |
303 | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
304 put_byte(pb, MAT_MARK_OUT); |
1183 | 305 put_byte(pb, 4); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
306 put_be32(pb, gxf->nb_fields); |
1183 | 307 |
308 /* estimated size */ | |
2282
47f5906c30cc
replaces hardcoded values by the equivalent enum definitions
aurel
parents:
2252
diff
changeset
|
309 put_byte(pb, MAT_SIZE); |
1183 | 310 put_byte(pb, 4); |
311 put_be32(pb, url_fsize(pb) / 1024); | |
312 | |
313 return updateSize(pb, pos); | |
314 } | |
315 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
316 static int gxf_write_track_description_section(AVFormatContext *s) |
1183 | 317 { |
4676 | 318 GXFContext *gxf = s->priv_data; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
319 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
320 int64_t pos; |
1183 | 321 int i; |
322 | |
323 pos = url_ftell(pb); | |
324 put_be16(pb, 0); /* size */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
325 for (i = 0; i < s->nb_streams; ++i) |
4676 | 326 gxf_write_track_description(s, s->streams[i]->priv_data, i); |
327 | |
328 gxf_write_track_description(s, &gxf->timecode_track, s->nb_streams); | |
329 | |
1183 | 330 return updateSize(pb, pos); |
331 } | |
332 | |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
333 static int gxf_write_map_packet(AVFormatContext *s, int rewrite) |
1183 | 334 { |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
335 GXFContext *gxf = s->priv_data; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
336 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
337 int64_t pos = url_ftell(pb); |
1183 | 338 |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
339 if (!rewrite) { |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
340 if (!(gxf->map_offsets_nb % 30)) { |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
341 gxf->map_offsets = av_realloc(gxf->map_offsets, |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
342 (gxf->map_offsets_nb+30)*sizeof(*gxf->map_offsets)); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
343 if (!gxf->map_offsets) { |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
344 av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n"); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
345 return -1; |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
346 } |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
347 } |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
348 gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
349 } |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
350 |
1183 | 351 gxf_write_packet_header(pb, PKT_MAP); |
352 | |
353 /* preamble */ | |
354 put_byte(pb, 0xE0); /* version */ | |
355 put_byte(pb, 0xFF); /* reserved */ | |
356 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
357 gxf_write_material_data_section(s); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
358 gxf_write_track_description_section(s); |
1183 | 359 |
360 return updatePacketSize(pb, pos); | |
361 } | |
362 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
363 static int gxf_write_flt_packet(AVFormatContext *s) |
1183 | 364 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
365 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
366 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
367 int64_t pos = url_ftell(pb); |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
368 int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1; |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
369 int flt_entries = gxf->nb_fields / fields_per_flt - 1; |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
370 int i = 0; |
1183 | 371 |
372 gxf_write_packet_header(pb, PKT_FLT); | |
373 | |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
374 put_le32(pb, fields_per_flt); /* number of fields */ |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
375 put_le32(pb, flt_entries); /* number of active flt entries */ |
1183 | 376 |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
377 if (gxf->flt_entries) { |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
378 for (i = 0; i < flt_entries; i++) |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
379 put_le32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]); |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
380 } |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
381 |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
382 for (; i < 1000; i++) |
1183 | 383 put_le32(pb, 0); |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
384 |
1183 | 385 return updatePacketSize(pb, pos); |
386 } | |
387 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
388 static int gxf_write_umf_material_description(AVFormatContext *s) |
1183 | 389 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
390 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
391 ByteIOContext *pb = s->pb; |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
392 int timecode_base = gxf->time_base.den == 60000 ? 60 : 50; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
393 |
4260 | 394 // XXX drop frame |
395 uint32_t timecode = | |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
396 gxf->nb_fields / (timecode_base * 3600) % 24 << 24 | // hours |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
397 gxf->nb_fields / (timecode_base * 60) % 60 << 16 | // minutes |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
398 gxf->nb_fields / timecode_base % 60 << 8 | // seconds |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
399 gxf->nb_fields % timecode_base; // fields |
4260 | 400 |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
401 put_le32(pb, gxf->flags); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
402 put_le32(pb, gxf->nb_fields); /* length of the longest track */ |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
403 put_le32(pb, gxf->nb_fields); /* length of the shortest track */ |
1183 | 404 put_le32(pb, 0); /* mark in */ |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
405 put_le32(pb, gxf->nb_fields); /* mark out */ |
1183 | 406 put_le32(pb, 0); /* timecode mark in */ |
4260 | 407 put_le32(pb, timecode); /* timecode mark out */ |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
408 put_le64(pb, s->timestamp); /* modification time */ |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
409 put_le64(pb, s->timestamp); /* creation time */ |
1183 | 410 put_le16(pb, 0); /* reserved */ |
411 put_le16(pb, 0); /* reserved */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
412 put_le16(pb, gxf->audio_tracks); |
4676 | 413 put_le16(pb, 1); /* timecode track count */ |
1183 | 414 put_le16(pb, 0); /* reserved */ |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
415 put_le16(pb, gxf->mpeg_tracks); |
1183 | 416 return 48; |
417 } | |
418 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
419 static int gxf_write_umf_payload(AVFormatContext *s) |
1183 | 420 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
421 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
422 ByteIOContext *pb = s->pb; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
423 |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
424 put_le32(pb, gxf->umf_length); /* total length of the umf data */ |
1183 | 425 put_le32(pb, 3); /* version */ |
4676 | 426 put_le32(pb, s->nb_streams+1); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
427 put_le32(pb, gxf->umf_track_offset); /* umf track section offset */ |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
428 put_le32(pb, gxf->umf_track_size); |
4676 | 429 put_le32(pb, s->nb_streams+1); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
430 put_le32(pb, gxf->umf_media_offset); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
431 put_le32(pb, gxf->umf_media_size); |
4673 | 432 put_le32(pb, gxf->umf_length); /* user data offset */ |
433 put_le32(pb, 0); /* user data size */ | |
1183 | 434 put_le32(pb, 0); /* reserved */ |
435 put_le32(pb, 0); /* reserved */ | |
436 return 48; | |
437 } | |
438 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
439 static int gxf_write_umf_track_description(AVFormatContext *s) |
1183 | 440 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
441 ByteIOContext *pb = s->pb; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
442 GXFContext *gxf = s->priv_data; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
443 int64_t pos = url_ftell(pb); |
1183 | 444 int i; |
445 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
446 gxf->umf_track_offset = pos - gxf->umf_start_offset; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
447 for (i = 0; i < s->nb_streams; ++i) { |
4675 | 448 GXFStreamContext *sc = s->streams[i]->priv_data; |
1183 | 449 put_le16(pb, sc->media_info); |
450 put_le16(pb, 1); | |
451 } | |
4676 | 452 |
453 put_le16(pb, gxf->timecode_track.media_info); | |
454 put_le16(pb, 1); | |
455 | |
1183 | 456 return url_ftell(pb) - pos; |
457 } | |
458 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
459 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, AVStream *st) |
1183 | 460 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
461 GXFStreamContext *sc = st->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
462 |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
463 if (st->codec->pix_fmt == PIX_FMT_YUV422P) |
1183 | 464 put_le32(pb, 2); |
465 else | |
466 put_le32(pb, 1); /* default to 420 */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
467 put_le32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */ |
1183 | 468 put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */ |
469 put_le32(pb, 1); /* I picture per GOP */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
470 put_le32(pb, sc->p_per_gop); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
471 put_le32(pb, sc->b_per_i_or_p); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
472 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) |
1183 | 473 put_le32(pb, 2); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
474 else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO) |
1183 | 475 put_le32(pb, 1); |
476 else | |
477 put_le32(pb, 0); | |
478 put_le32(pb, 0); /* reserved */ | |
479 return 32; | |
480 } | |
481 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
482 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *sc) |
1183 | 483 { |
4676 | 484 put_le32(pb, 1); /* non drop frame */ |
485 put_le32(pb, 0); /* reserved */ | |
486 put_le32(pb, 0); /* reserved */ | |
487 put_le32(pb, 0); /* reserved */ | |
488 put_le32(pb, 0); /* reserved */ | |
489 put_le32(pb, 0); /* reserved */ | |
490 put_le32(pb, 0); /* reserved */ | |
491 put_le32(pb, 0); /* reserved */ | |
1183 | 492 return 32; |
493 } | |
494 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
495 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *sc) |
1183 | 496 { |
497 int i; | |
498 | |
499 for (i = 0; i < 8; i++) { | |
500 put_be32(pb, 0); | |
501 } | |
502 return 32; | |
503 } | |
504 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
505 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *sc) |
1183 | 506 { |
507 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */ | |
508 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */ | |
509 put_le32(pb, 0); /* number of fields over which to ramp up sound level */ | |
510 put_le32(pb, 0); /* number of fields over which to ramp down sound level */ | |
511 put_le32(pb, 0); /* reserved */ | |
512 put_le32(pb, 0); /* reserved */ | |
513 return 32; | |
514 } | |
515 | |
516 #if 0 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
517 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *sc) |
1183 | 518 { |
519 put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */ | |
520 put_be64(pb, 0); /* FIXME FLOAT max luma quant level */ | |
521 put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */ | |
522 put_be64(pb, 0); /* FIXME FLOAT min luma quant level */ | |
523 return 32; | |
524 } | |
525 #endif | |
526 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
527 static int gxf_write_umf_media_description(AVFormatContext *s) |
1183 | 528 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
529 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
530 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
531 int64_t pos; |
4681
83c634df5c79
change path pattern to be similar to existing files, and remove useless buffer
bcoudurier
parents:
4680
diff
changeset
|
532 int i, j; |
1183 | 533 |
534 pos = url_ftell(pb); | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
535 gxf->umf_media_offset = pos - gxf->umf_start_offset; |
4676 | 536 for (i = 0; i <= s->nb_streams; ++i) { |
537 GXFStreamContext *sc; | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
538 int64_t startpos, curpos; |
1183 | 539 |
4676 | 540 if (i == s->nb_streams) |
541 sc = &gxf->timecode_track; | |
542 else | |
543 sc = s->streams[i]->priv_data; | |
544 | |
1183 | 545 startpos = url_ftell(pb); |
546 put_le16(pb, 0); /* length */ | |
547 put_le16(pb, sc->media_info); | |
548 put_le16(pb, 0); /* reserved */ | |
549 put_le16(pb, 0); /* reserved */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
550 put_le32(pb, gxf->nb_fields); |
1183 | 551 put_le32(pb, 0); /* attributes rw, ro */ |
552 put_le32(pb, 0); /* mark in */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
553 put_le32(pb, gxf->nb_fields); /* mark out */ |
4681
83c634df5c79
change path pattern to be similar to existing files, and remove useless buffer
bcoudurier
parents:
4680
diff
changeset
|
554 put_buffer(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN)); |
1183 | 555 put_be16(pb, sc->media_info); |
4681
83c634df5c79
change path pattern to be similar to existing files, and remove useless buffer
bcoudurier
parents:
4680
diff
changeset
|
556 for (j = sizeof(ES_NAME_PATTERN)+2; j < 88; j++) |
83c634df5c79
change path pattern to be similar to existing files, and remove useless buffer
bcoudurier
parents:
4680
diff
changeset
|
557 put_byte(pb, 0); |
1183 | 558 put_le32(pb, sc->track_type); |
559 put_le32(pb, sc->sample_rate); | |
560 put_le32(pb, sc->sample_size); | |
561 put_le32(pb, 0); /* reserved */ | |
4676 | 562 |
563 if (sc == &gxf->timecode_track) | |
564 gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */ | |
565 else { | |
566 AVStream *st = s->streams[i]; | |
4682 | 567 switch (st->codec->codec_id) { |
568 case CODEC_ID_MPEG2VIDEO: | |
569 gxf_write_umf_media_mpeg(pb, st); | |
570 break; | |
571 case CODEC_ID_PCM_S16LE: | |
572 gxf_write_umf_media_audio(pb, sc); | |
573 break; | |
574 case CODEC_ID_DVVIDEO: | |
575 gxf_write_umf_media_dv(pb, sc); | |
576 break; | |
577 } | |
1183 | 578 } |
4676 | 579 |
1183 | 580 curpos = url_ftell(pb); |
581 url_fseek(pb, startpos, SEEK_SET); | |
582 put_le16(pb, curpos - startpos); | |
583 url_fseek(pb, curpos, SEEK_SET); | |
584 } | |
585 return url_ftell(pb) - pos; | |
586 } | |
587 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
588 static int gxf_write_umf_packet(AVFormatContext *s) |
1183 | 589 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
590 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
591 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
592 int64_t pos = url_ftell(pb); |
1183 | 593 |
594 gxf_write_packet_header(pb, PKT_UMF); | |
595 | |
596 /* preamble */ | |
597 put_byte(pb, 3); /* first and last (only) packet */ | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
598 put_be32(pb, gxf->umf_length); /* data length */ |
1183 | 599 |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
600 gxf->umf_start_offset = url_ftell(pb); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
601 gxf_write_umf_payload(s); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
602 gxf_write_umf_material_description(s); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
603 gxf->umf_track_size = gxf_write_umf_track_description(s); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
604 gxf->umf_media_size = gxf_write_umf_media_description(s); |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
605 gxf->umf_length = url_ftell(pb) - gxf->umf_start_offset; |
1183 | 606 return updatePacketSize(pb, pos); |
607 } | |
608 | |
4401 | 609 static const int GXF_samples_per_frame[] = { 32768, 0 }; |
610 | |
4676 | 611 static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc) |
612 { | |
613 if (!vsc) | |
614 return; | |
615 | |
616 sc->media_type = vsc->sample_rate == 60 ? 7 : 8; | |
617 sc->sample_rate = vsc->sample_rate; | |
618 sc->media_info = ('T'<<8) | '0'; | |
619 sc->track_type = 3; | |
620 sc->frame_rate_index = vsc->frame_rate_index; | |
621 sc->lines_index = vsc->lines_index; | |
622 sc->sample_size = 16; | |
623 sc->fields = vsc->fields; | |
624 } | |
625 | |
1183 | 626 static int gxf_write_header(AVFormatContext *s) |
627 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2337
diff
changeset
|
628 ByteIOContext *pb = s->pb; |
1183 | 629 GXFContext *gxf = s->priv_data; |
4676 | 630 GXFStreamContext *vsc = NULL; |
4675 | 631 uint8_t tracks[255] = {0}; |
632 int i, media_info = 0; | |
1183 | 633 |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
634 if (url_is_streamed(pb)) { |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
635 av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome"); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
636 return -1; |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
637 } |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
638 |
1183 | 639 gxf->flags |= 0x00080000; /* material is simple clip */ |
640 for (i = 0; i < s->nb_streams; ++i) { | |
641 AVStream *st = s->streams[i]; | |
4329 | 642 GXFStreamContext *sc = av_mallocz(sizeof(*sc)); |
643 if (!sc) | |
644 return AVERROR(ENOMEM); | |
645 st->priv_data = sc; | |
1183 | 646 |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
647 sc->media_type = codec_get_tag(gxf_media_types, st->codec->codec_id); |
1183 | 648 if (st->codec->codec_type == CODEC_TYPE_AUDIO) { |
649 if (st->codec->codec_id != CODEC_ID_PCM_S16LE) { | |
650 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n"); | |
651 return -1; | |
652 } | |
653 if (st->codec->sample_rate != 48000) { | |
654 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n"); | |
655 return -1; | |
656 } | |
657 if (st->codec->channels != 1) { | |
658 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n"); | |
659 return -1; | |
660 } | |
661 sc->track_type = 2; | |
662 sc->sample_rate = st->codec->sample_rate; | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
663 av_set_pts_info(st, 64, 1, sc->sample_rate); |
1183 | 664 sc->sample_size = 16; |
665 sc->frame_rate_index = -2; | |
666 sc->lines_index = -2; | |
667 sc->fields = -2; | |
668 gxf->audio_tracks++; | |
669 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */ | |
4675 | 670 media_info = 'A'; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
671 } else if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
672 if (i != 0) { |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
673 av_log(s, AV_LOG_ERROR, "video stream must be the first track\n"); |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
674 return -1; |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
675 } |
1183 | 676 /* FIXME check from time_base ? */ |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
677 if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */ |
1183 | 678 sc->frame_rate_index = 5; |
679 sc->sample_rate = 60; | |
680 gxf->flags |= 0x00000080; | |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
681 gxf->time_base = (AVRational){ 1001, 60000 }; |
1183 | 682 } else { /* assume PAL */ |
683 sc->frame_rate_index = 6; | |
684 sc->media_type++; | |
685 sc->sample_rate = 50; | |
686 gxf->flags |= 0x00000040; | |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
687 gxf->time_base = (AVRational){ 1, 50 }; |
1183 | 688 } |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
689 av_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
690 if (gxf_find_lines_index(st) < 0) |
1183 | 691 sc->lines_index = -1; |
692 sc->sample_size = st->codec->bit_rate; | |
693 sc->fields = 2; /* interlaced */ | |
4676 | 694 |
695 vsc = sc; | |
696 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
697 switch (st->codec->codec_id) { |
4675 | 698 case CODEC_ID_MJPEG: |
699 sc->track_type = 1; | |
700 gxf->flags |= 0x00004000; | |
701 media_info = 'J'; | |
702 break; | |
703 case CODEC_ID_MPEG1VIDEO: | |
704 sc->track_type = 9; | |
705 gxf->mpeg_tracks++; | |
706 media_info = 'L'; | |
707 break; | |
1183 | 708 case CODEC_ID_MPEG2VIDEO: |
1262 | 709 sc->first_gop_closed = -1; |
1183 | 710 sc->track_type = 4; |
711 gxf->mpeg_tracks++; | |
712 gxf->flags |= 0x00008000; | |
4675 | 713 media_info = 'M'; |
1183 | 714 break; |
715 case CODEC_ID_DVVIDEO: | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
716 if (st->codec->pix_fmt == PIX_FMT_YUV422P) { |
1183 | 717 sc->media_type += 2; |
718 sc->track_type = 6; | |
719 gxf->flags |= 0x00002000; | |
4675 | 720 media_info = 'E'; |
1183 | 721 } else { |
722 sc->track_type = 5; | |
723 gxf->flags |= 0x00001000; | |
4675 | 724 media_info = 'D'; |
1183 | 725 } |
726 break; | |
727 default: | |
1401 | 728 av_log(s, AV_LOG_ERROR, "video codec not supported\n"); |
1183 | 729 return -1; |
730 } | |
731 } | |
4675 | 732 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */ |
733 sc->media_info = media_info<<8 | ('0'+tracks[media_info]++); | |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
734 sc->order = s->nb_streams - st->index; |
1183 | 735 } |
4401 | 736 |
737 if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0) | |
738 return -1; | |
739 | |
4676 | 740 gxf_init_timecode_track(&gxf->timecode_track, vsc); |
741 gxf->flags |= 0x200000; // time code track is non-drop frame | |
742 | |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
743 gxf_write_map_packet(s, 0); |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
744 gxf_write_flt_packet(s); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
745 gxf_write_umf_packet(s); |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
746 |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
747 gxf->packet_count = 3; |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
748 |
1183 | 749 put_flush_packet(pb); |
750 return 0; | |
751 } | |
752 | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
753 static int gxf_write_eos_packet(ByteIOContext *pb) |
1183 | 754 { |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
755 int64_t pos = url_ftell(pb); |
1183 | 756 |
757 gxf_write_packet_header(pb, PKT_EOS); | |
758 return updatePacketSize(pb, pos); | |
759 } | |
760 | |
761 static int gxf_write_trailer(AVFormatContext *s) | |
762 { | |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
763 GXFContext *gxf = s->priv_data; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2337
diff
changeset
|
764 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
765 int64_t end; |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
766 int i; |
1183 | 767 |
4401 | 768 ff_audio_interleave_close(s); |
1183 | 769 |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
770 gxf_write_eos_packet(pb); |
1183 | 771 end = url_ftell(pb); |
772 url_fseek(pb, 0, SEEK_SET); | |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
773 /* overwrite map, flt and umf packets with new values */ |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
774 gxf_write_map_packet(s, 1); |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
775 gxf_write_flt_packet(s); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
776 gxf_write_umf_packet(s); |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
777 put_flush_packet(pb); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
778 /* update duration in all map packets */ |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
779 for (i = 1; i < gxf->map_offsets_nb; i++) { |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
780 url_fseek(pb, gxf->map_offsets[i], SEEK_SET); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
781 gxf_write_map_packet(s, 1); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
782 put_flush_packet(pb); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
783 } |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
784 |
1183 | 785 url_fseek(pb, end, SEEK_SET); |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
786 |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
787 av_freep(&gxf->flt_entries); |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
788 av_freep(&gxf->map_offsets); |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
789 |
1183 | 790 return 0; |
791 } | |
792 | |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
793 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size) |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
794 { |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
795 uint32_t c=-1; |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
796 int i; |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
797 for(i=0; i<size-4 && c!=0x100; i++){ |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
798 c = (c<<8) + buf[i]; |
1262 | 799 if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */ |
1261 | 800 sc->first_gop_closed= (buf[i+4]>>6)&1; |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
801 } |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
802 return (buf[i+1]>>3)&7; |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
803 } |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
804 |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
805 static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size) |
1183 | 806 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
807 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
808 ByteIOContext *pb = s->pb; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
809 AVStream *st = s->streams[pkt->stream_index]; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
810 GXFStreamContext *sc = st->priv_data; |
4480
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
811 unsigned field_nb; |
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
812 /* If the video is frame-encoded, the frame numbers shall be represented by |
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
813 * even field numbers. |
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
814 * see SMPTE360M-2004 6.4.2.1.3 Media field number */ |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
815 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
816 field_nb = gxf->nb_fields; |
4480
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
817 } else { |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
818 field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den, |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
819 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP); |
4480
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
820 } |
1183 | 821 |
822 put_byte(pb, sc->media_type); | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
823 put_byte(pb, st->index); |
4480
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
824 put_be32(pb, field_nb); |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
825 if (st->codec->codec_type == CODEC_TYPE_AUDIO) { |
1183 | 826 put_be16(pb, 0); |
827 put_be16(pb, size / 2); | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
828 } else if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) { |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
829 int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size); |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
830 if (frame_type == FF_I_TYPE) { |
1183 | 831 put_byte(pb, 0x0d); |
832 sc->iframes++; | |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
833 } else if (frame_type == FF_B_TYPE) { |
1183 | 834 put_byte(pb, 0x0f); |
835 sc->bframes++; | |
836 } else { | |
837 put_byte(pb, 0x0e); | |
838 sc->pframes++; | |
839 } | |
840 put_be24(pb, size); | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
841 } else if (st->codec->codec_id == CODEC_ID_DVVIDEO) { |
1183 | 842 put_byte(pb, size / 4096); |
843 put_be24(pb, 0); | |
844 } else | |
845 put_be32(pb, size); | |
4480
b88326454853
use correct field number for video according to specs, patch by Thierry Foucu, tfoucu at gmail dot com
bcoudurier
parents:
4479
diff
changeset
|
846 put_be32(pb, field_nb); |
1183 | 847 put_byte(pb, 1); /* flags */ |
848 put_byte(pb, 0); /* reserved */ | |
849 return 16; | |
850 } | |
851 | |
4674 | 852 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt) |
1183 | 853 { |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
854 GXFContext *gxf = s->priv_data; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
855 ByteIOContext *pb = s->pb; |
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
856 AVStream *st = s->streams[pkt->stream_index]; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3424
diff
changeset
|
857 int64_t pos = url_ftell(pb); |
1183 | 858 int padding = 0; |
859 | |
860 gxf_write_packet_header(pb, PKT_MEDIA); | |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
861 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */ |
1183 | 862 padding = 4 - pkt->size % 4; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
863 else if (st->codec->codec_type == CODEC_TYPE_AUDIO) |
1212 | 864 padding = GXF_AUDIO_PACKET_SIZE - pkt->size; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
865 gxf_write_media_preamble(s, pkt, pkt->size + padding); |
1212 | 866 put_buffer(pb, pkt->data, pkt->size); |
867 gxf_write_padding(pb, padding); | |
4262
f146ce39977a
Do not use avctx->frame_number which might not be set,
bcoudurier
parents:
4261
diff
changeset
|
868 |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
869 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
870 if (!(gxf->flt_entries_nb % 500)) { |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
871 gxf->flt_entries = av_realloc(gxf->flt_entries, |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
872 (gxf->flt_entries_nb+500)*sizeof(*gxf->flt_entries)); |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
873 if (!gxf->flt_entries) { |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
874 av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n"); |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
875 return -1; |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
876 } |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
877 } |
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
878 gxf->flt_entries[gxf->flt_entries_nb++] = url_ftell(pb) / 1024; |
4672
f5d61e72fec5
uniformize variable names and remove duplicating fields in private structs
bcoudurier
parents:
4671
diff
changeset
|
879 gxf->nb_fields += 2; // count fields |
4678
65036104b950
write flt packet, might need some tweaking but it works
bcoudurier
parents:
4677
diff
changeset
|
880 } |
4262
f146ce39977a
Do not use avctx->frame_number which might not be set,
bcoudurier
parents:
4261
diff
changeset
|
881 |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
882 updatePacketSize(pb, pos); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
883 |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
884 gxf->packet_count++; |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
885 if (gxf->packet_count == 100) { |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
886 gxf_write_map_packet(s, 0); |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
887 gxf->packet_count = 0; |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
888 } |
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
889 |
4674 | 890 put_flush_packet(pb); |
1183 | 891 |
4679
697b6a0366a6
write map packet every 100 packets according to specs
bcoudurier
parents:
4678
diff
changeset
|
892 return 0; |
1183 | 893 } |
894 | |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
895 static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur) |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
896 { |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
897 GXFContext *gxf = s->priv_data; |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
898 AVPacket *pkt[2] = { cur, next }; |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
899 int i, field_nb[2]; |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
900 GXFStreamContext *sc[2]; |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
901 |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
902 for (i = 0; i < 2; i++) { |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
903 AVStream *st = s->streams[pkt[i]->stream_index]; |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
904 sc[i] = st->priv_data; |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
905 if (st->codec->codec_type == CODEC_TYPE_AUDIO) { |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
906 field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den, |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
907 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP); |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
908 field_nb[i] &= ~1; // compare against even field number because audio must be before video |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
909 } else |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
910 field_nb[i] = pkt[i]->dts; // dts are field based |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
911 } |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
912 |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
913 return field_nb[1] > field_nb[0] || |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
914 (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order); |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
915 } |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
916 |
1183 | 917 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) |
918 { | |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
919 if (pkt && s->streams[pkt->stream_index]->codec->codec_type == CODEC_TYPE_VIDEO) |
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
920 pkt->duration = 2; // enforce 2 fields |
4426
7854590fb1fd
rename ff_audio_interleave to ff_audio_rechunk_interleave
bcoudurier
parents:
4401
diff
changeset
|
921 return ff_audio_rechunk_interleave(s, out, pkt, flush, |
4677
96c24e93b7d8
correctly interleave audio and video according to specs
bcoudurier
parents:
4676
diff
changeset
|
922 av_interleave_packet_per_dts, gxf_compare_field_nb); |
1183 | 923 } |
924 | |
925 AVOutputFormat gxf_muxer = { | |
926 "gxf", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3368
diff
changeset
|
927 NULL_IF_CONFIG_SMALL("GXF format"), |
1183 | 928 NULL, |
929 "gxf", | |
930 sizeof(GXFContext), | |
931 CODEC_ID_PCM_S16LE, | |
932 CODEC_ID_MPEG2VIDEO, | |
933 gxf_write_header, | |
934 gxf_write_packet, | |
935 gxf_write_trailer, | |
936 0, | |
937 NULL, | |
938 gxf_interleave_packet, | |
939 }; |