1183
|
1 /*
|
|
2 * GXF muxer.
|
|
3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or modify
|
|
6 * it under the terms of the GNU General Public License as published by
|
|
7 * the Free Software Foundation; either version 2 of the License, or
|
|
8 * (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
|
|
13 * GNU General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License
|
|
16 * along with this program; if not, write to the Free Software
|
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19
|
|
20 #include "avformat.h"
|
|
21 #include "gxf.h"
|
|
22 #include "riff.h"
|
|
23
|
|
24 #define GXF_AUDIO_PACKET_SIZE 65536
|
|
25
|
|
26 typedef struct GXFStreamContext {
|
|
27 AVCodecContext *codec;
|
|
28 FifoBuffer audio_buffer;
|
|
29 uint32_t track_type;
|
|
30 uint32_t sample_size;
|
|
31 uint32_t sample_rate;
|
|
32 uint16_t media_type;
|
|
33 uint16_t media_info;
|
|
34 uint8_t index;
|
|
35 int frame_rate_index;
|
|
36 int lines_index;
|
|
37 int fields;
|
|
38 int iframes;
|
|
39 int pframes;
|
|
40 int bframes;
|
|
41 int p_per_gop;
|
|
42 int b_per_gop;
|
|
43 } GXFStreamContext;
|
|
44
|
|
45 typedef struct GXFContext {
|
|
46 uint32_t nb_frames;
|
|
47 uint32_t material_flags;
|
|
48 uint16_t audio_tracks;
|
|
49 uint16_t mpeg_tracks;
|
|
50 int64_t creation_time;
|
|
51 uint32_t umf_start_offset;
|
|
52 uint32_t umf_track_offset;
|
|
53 uint32_t umf_media_offset;
|
|
54 uint32_t umf_user_data_offset;
|
|
55 uint32_t umf_user_data_size;
|
|
56 uint32_t umf_length;
|
|
57 uint16_t umf_track_size;
|
|
58 uint16_t umf_media_size;
|
|
59 int audio_written;
|
|
60 int sample_rate;
|
|
61 int field_number;
|
|
62 int flags;
|
|
63 AVFormatContext *fc;
|
|
64 GXFStreamContext streams[48];
|
|
65 } GXFContext;
|
|
66
|
|
67 typedef struct GXF_Lines {
|
|
68 int height;
|
|
69 int index;
|
|
70 } GXF_Lines;
|
|
71
|
|
72
|
|
73 /* FIXME check if it is relevant */
|
|
74 static const GXF_Lines gxf_lines_tab[] = {
|
|
75 { 480, 1 }, /* NTSC */
|
|
76 { 512, 1 }, /* NTSC + VBI */
|
|
77 { 576, 2 }, /* PAL */
|
|
78 { 608, 2 }, /* PAL + VBI */
|
|
79 { 1080, 4 },
|
|
80 { 720, 6 },
|
|
81 };
|
|
82
|
|
83 static const CodecTag gxf_media_types[] = {
|
|
84 { CODEC_ID_MJPEG , 3 }, /* NTSC */
|
|
85 { CODEC_ID_MJPEG , 4 }, /* PAL */
|
|
86 { CODEC_ID_PCM_S24LE , 9 },
|
|
87 { CODEC_ID_PCM_S16LE , 10 },
|
|
88 { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
|
|
89 { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
|
|
90 { CODEC_ID_DVVIDEO , 13 }, /* NTSC */
|
|
91 { CODEC_ID_DVVIDEO , 14 }, /* PAL */
|
|
92 { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
|
|
93 { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
|
|
94 { CODEC_ID_AC3 , 17 },
|
|
95 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
|
|
96 { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
|
|
97 { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
|
|
98 { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
|
|
99 { 0, 0 },
|
|
100 };
|
|
101
|
|
102 #define SERVER_PATH "/space/"
|
|
103 #define ES_NAME_PATTERN "ES."
|
|
104
|
|
105 static int gxf_find_lines_index(GXFStreamContext *ctx)
|
|
106 {
|
|
107 int i;
|
|
108
|
|
109 for (i = 0; i < 6; ++i) {
|
|
110 if (ctx->codec->height == gxf_lines_tab[i].height) {
|
|
111 ctx->lines_index = gxf_lines_tab[i].index;
|
|
112 return 0;
|
|
113 }
|
|
114 }
|
|
115 return -1;
|
|
116 }
|
|
117
|
|
118 static void gxf_write_padding(ByteIOContext *pb, offset_t to_pad)
|
|
119 {
|
1213
|
120 for (; to_pad > 0; to_pad--) {
|
1183
|
121 put_byte(pb, 0);
|
|
122 }
|
|
123 }
|
|
124
|
|
125 static offset_t updatePacketSize(ByteIOContext *pb, offset_t pos)
|
|
126 {
|
|
127 offset_t curpos;
|
|
128 int size;
|
|
129
|
|
130 size = url_ftell(pb) - pos;
|
|
131 if (size % 4) {
|
|
132 gxf_write_padding(pb, 4 - size % 4);
|
|
133 size = url_ftell(pb) - pos;
|
|
134 }
|
|
135 curpos = url_ftell(pb);
|
|
136 url_fseek(pb, pos + 6, SEEK_SET);
|
|
137 put_be32(pb, size);
|
|
138 url_fseek(pb, curpos, SEEK_SET);
|
|
139 return curpos - pos;
|
|
140 }
|
|
141
|
|
142 static offset_t updateSize(ByteIOContext *pb, offset_t pos)
|
|
143 {
|
|
144 offset_t curpos;
|
|
145
|
|
146 curpos = url_ftell(pb);
|
|
147 url_fseek(pb, pos, SEEK_SET);
|
|
148 put_be16(pb, curpos - pos - 2);
|
|
149 url_fseek(pb, curpos, SEEK_SET);
|
|
150 return curpos - pos;
|
|
151 }
|
|
152
|
|
153 static void gxf_write_packet_header(ByteIOContext *pb, pkt_type_t type)
|
|
154 {
|
|
155 put_be32(pb, 0); /* packet leader for synchro */
|
|
156 put_byte(pb, 1);
|
|
157 put_byte(pb, type); /* map packet */
|
|
158 put_be32(pb, 0); /* size */
|
|
159 put_be32(pb, 0); /* reserved */
|
|
160 put_byte(pb, 0xE1); /* trailer 1 */
|
|
161 put_byte(pb, 0xE2); /* trailer 2 */
|
|
162 }
|
|
163
|
|
164 static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
|
|
165 {
|
|
166 char buffer[1024];
|
|
167 int size;
|
|
168
|
|
169 if (ctx->iframes) {
|
|
170 ctx->p_per_gop = ctx->pframes / ctx->iframes;
|
|
171 if (ctx->pframes % ctx->iframes)
|
|
172 ctx->p_per_gop++;
|
|
173 if (ctx->pframes)
|
|
174 ctx->b_per_gop = ctx->bframes / ctx->pframes;
|
|
175 if (ctx->p_per_gop > 9)
|
|
176 ctx->p_per_gop = 9; /* ensure value won't take more than one char */
|
|
177 if (ctx->b_per_gop > 9)
|
|
178 ctx->b_per_gop = 9; /* ensure value won't take more than one char */
|
|
179 }
|
|
180 size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
|
|
181 "Pix 0\nCf %d\nCg 1\nSl 7\nnl16 %d\nVi 1\nf1 1\n",
|
|
182 (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop,
|
|
183 ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1,
|
|
184 ctx->codec->height / 16);
|
|
185 put_byte(pb, 0x4F);
|
|
186 put_byte(pb, size + 1);
|
|
187 put_buffer(pb, (uint8_t *)buffer, size + 1);
|
|
188 return size + 3;
|
|
189 }
|
|
190
|
|
191 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
|
|
192 {
|
|
193 /* FIXME implement that */
|
|
194 put_byte(pb, 0); /* fields */
|
|
195 put_byte(pb, 0); /* seconds */
|
|
196 put_byte(pb, 0); /* minutes */
|
|
197 put_byte(pb, 0); /* flags + hours */
|
|
198 /* reserved */
|
|
199 put_be32(pb, 0);
|
|
200 return 8;
|
|
201 }
|
|
202
|
|
203 static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stream)
|
|
204 {
|
|
205 offset_t pos;
|
|
206
|
|
207 /* track description section */
|
|
208 put_byte(pb, stream->media_type + 0x80);
|
|
209 put_byte(pb, stream->index + 0xC0);
|
|
210
|
|
211 pos = url_ftell(pb);
|
|
212 put_be16(pb, 0); /* size */
|
|
213
|
|
214 /* media file name */
|
|
215 put_byte(pb, 0x4C);
|
|
216 put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
|
|
217 put_tag(pb, ES_NAME_PATTERN);
|
|
218 put_be16(pb, stream->media_info);
|
|
219 put_byte(pb, 0);
|
|
220
|
|
221 if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
|
|
222 /* auxiliary information */
|
|
223 put_byte(pb, 0x4D);
|
|
224 put_byte(pb, 8);
|
|
225 if (stream->codec->codec_id == CODEC_ID_NONE)
|
|
226 gxf_write_timecode_auxiliary(pb, stream);
|
|
227 else
|
|
228 put_le64(pb, 0);
|
|
229 }
|
|
230
|
|
231 /* file system version */
|
|
232 put_byte(pb, 0x4E);
|
|
233 put_byte(pb, 4);
|
|
234 put_be32(pb, 0);
|
|
235
|
|
236 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
|
|
237 gxf_write_mpeg_auxiliary(pb, stream);
|
|
238
|
|
239 /* frame rate */
|
|
240 put_byte(pb, 0x50);
|
|
241 put_byte(pb, 4);
|
|
242 put_be32(pb, stream->frame_rate_index);
|
|
243
|
|
244 /* lines per frame */
|
|
245 put_byte(pb, 0x51);
|
|
246 put_byte(pb, 4);
|
|
247 put_be32(pb, stream->lines_index);
|
|
248
|
|
249 /* fields per frame */
|
|
250 put_byte(pb, 0x52);
|
|
251 put_byte(pb, 4);
|
|
252 put_be32(pb, stream->fields);
|
|
253
|
|
254 return updateSize(pb, pos);
|
|
255 }
|
|
256
|
|
257 static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
|
|
258 {
|
|
259 offset_t pos;
|
|
260 const char *filename = strrchr(ctx->fc->filename, '/');
|
|
261
|
|
262 pos = url_ftell(pb);
|
|
263 put_be16(pb, 0); /* size */
|
|
264
|
|
265 /* name */
|
|
266 if (filename)
|
|
267 filename++;
|
|
268 else
|
|
269 filename = ctx->fc->filename;
|
|
270 put_byte(pb, 0x40);
|
|
271 put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
|
|
272 put_tag(pb, SERVER_PATH);
|
|
273 put_tag(pb, filename);
|
|
274 put_byte(pb, 0);
|
|
275
|
|
276 /* first field */
|
|
277 put_byte(pb, 0x41);
|
|
278 put_byte(pb, 4);
|
|
279 put_be32(pb, 0);
|
|
280
|
|
281 /* last field */
|
|
282 put_byte(pb, 0x42);
|
|
283 put_byte(pb, 4);
|
|
284 put_be32(pb, ctx->nb_frames);
|
|
285
|
|
286 /* reserved */
|
|
287 put_byte(pb, 0x43);
|
|
288 put_byte(pb, 4);
|
|
289 put_be32(pb, 0);
|
|
290
|
|
291 put_byte(pb, 0x44);
|
|
292 put_byte(pb, 4);
|
|
293 put_be32(pb, ctx->nb_frames);
|
|
294
|
|
295 /* estimated size */
|
|
296 put_byte(pb, 0x45);
|
|
297 put_byte(pb, 4);
|
|
298 put_be32(pb, url_fsize(pb) / 1024);
|
|
299
|
|
300 return updateSize(pb, pos);
|
|
301 }
|
|
302
|
|
303 static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx)
|
|
304 {
|
|
305 offset_t pos;
|
|
306 int i;
|
|
307
|
|
308 pos = url_ftell(pb);
|
|
309 put_be16(pb, 0); /* size */
|
|
310 for (i = 0; i < ctx->fc->nb_streams; ++i)
|
|
311 gxf_write_track_description(pb, &ctx->streams[i]);
|
|
312 return updateSize(pb, pos);
|
|
313 }
|
|
314
|
|
315 static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
|
|
316 {
|
|
317 offset_t pos = url_ftell(pb);
|
|
318
|
|
319 gxf_write_packet_header(pb, PKT_MAP);
|
|
320
|
|
321 /* preamble */
|
|
322 put_byte(pb, 0xE0); /* version */
|
|
323 put_byte(pb, 0xFF); /* reserved */
|
|
324
|
|
325 gxf_write_material_data_section(pb, ctx);
|
|
326 gxf_write_track_description_section(pb, ctx);
|
|
327
|
|
328 return updatePacketSize(pb, pos);
|
|
329 }
|
|
330
|
|
331 #if 0
|
|
332 static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx)
|
|
333 {
|
|
334 offset_t pos = url_ftell(pb);
|
|
335 int i;
|
|
336
|
|
337 gxf_write_packet_header(pb, PKT_FLT);
|
|
338
|
|
339 put_le32(pb, 1000); /* number of fields */
|
|
340 put_le32(pb, 0); /* number of active flt entries */
|
|
341
|
|
342 for (i = 0; i < 1000; ++i) {
|
|
343 put_le32(pb, 0);
|
|
344 }
|
|
345 return updatePacketSize(pb, pos);
|
|
346 }
|
|
347 #endif
|
|
348
|
|
349 static int gxf_write_umf_material_description(ByteIOContext *pb, GXFContext *ctx)
|
|
350 {
|
|
351 put_le32(pb, ctx->flags);
|
|
352 put_le32(pb, ctx->nb_frames); /* length of the longest track */
|
|
353 put_le32(pb, ctx->nb_frames); /* length of the shortest track */
|
|
354 put_le32(pb, 0); /* mark in */
|
|
355 put_le32(pb, ctx->nb_frames); /* mark out */
|
|
356 put_le32(pb, 0); /* timecode mark in */
|
|
357 put_le32(pb, ctx->nb_frames); /* timecode mark out */
|
|
358 put_le64(pb, ctx->fc->timestamp); /* modification time */
|
|
359 put_le64(pb, ctx->fc->timestamp); /* creation time */
|
|
360 put_le16(pb, 0); /* reserved */
|
|
361 put_le16(pb, 0); /* reserved */
|
|
362 put_le16(pb, ctx->audio_tracks);
|
|
363 put_le16(pb, 0); /* timecode track count */
|
|
364 put_le16(pb, 0); /* reserved */
|
|
365 put_le16(pb, ctx->mpeg_tracks);
|
|
366 return 48;
|
|
367 }
|
|
368
|
|
369 static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx)
|
|
370 {
|
|
371 put_le32(pb, ctx->umf_length); /* total length of the umf data */
|
|
372 put_le32(pb, 3); /* version */
|
|
373 put_le32(pb, ctx->fc->nb_streams);
|
|
374 put_le32(pb, ctx->umf_track_offset); /* umf track section offset */
|
|
375 put_le32(pb, ctx->umf_track_size);
|
|
376 put_le32(pb, ctx->fc->nb_streams);
|
|
377 put_le32(pb, ctx->umf_media_offset);
|
|
378 put_le32(pb, ctx->umf_media_size);
|
|
379 put_le32(pb, ctx->umf_user_data_offset); /* user data offset */
|
|
380 put_le32(pb, ctx->umf_user_data_size); /* user data size */
|
|
381 put_le32(pb, 0); /* reserved */
|
|
382 put_le32(pb, 0); /* reserved */
|
|
383 return 48;
|
|
384 }
|
|
385
|
|
386 static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
|
|
387 {
|
|
388 offset_t pos = url_ftell(pb);
|
|
389 int tracks[255]={0};
|
|
390 int i;
|
|
391
|
|
392 ctx->umf_track_offset = pos - ctx->umf_start_offset;
|
|
393 for (i = 0; i < ctx->fc->nb_streams; ++i) {
|
|
394 AVStream *st = ctx->fc->streams[i];
|
|
395 GXFStreamContext *sc = &ctx->streams[i];
|
|
396 int id = 0;
|
|
397
|
|
398 switch (st->codec->codec_id) {
|
|
399 case CODEC_ID_MPEG1VIDEO: id= 'L'; break;
|
|
400 case CODEC_ID_MPEG2VIDEO: id= 'M'; break;
|
|
401 case CODEC_ID_PCM_S16LE: id= 'A'; break;
|
|
402 case CODEC_ID_DVVIDEO: id= sc->track_type == 6 ? 'E' : 'D'; break;
|
|
403 case CODEC_ID_MJPEG: id= 'V'; break;
|
|
404 }
|
|
405 sc->media_info= id << 8;
|
|
406 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
|
|
407 sc->media_info |= '0' + (tracks[id]++);
|
|
408 put_le16(pb, sc->media_info);
|
|
409 put_le16(pb, 1);
|
|
410 }
|
|
411 return url_ftell(pb) - pos;
|
|
412 }
|
|
413
|
|
414 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, GXFStreamContext *stream)
|
|
415 {
|
|
416 if (stream->codec->pix_fmt == PIX_FMT_YUV422P)
|
|
417 put_le32(pb, 2);
|
|
418 else
|
|
419 put_le32(pb, 1); /* default to 420 */
|
|
420 put_le32(pb, 1); /* closed = 1, open = 0, unknown = 255 */
|
|
421 put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
|
|
422 put_le32(pb, 1); /* I picture per GOP */
|
|
423 put_le32(pb, stream->p_per_gop);
|
|
424 put_le32(pb, stream->b_per_gop);
|
|
425 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
|
|
426 put_le32(pb, 2);
|
|
427 else if (stream->codec->codec_id == CODEC_ID_MPEG1VIDEO)
|
|
428 put_le32(pb, 1);
|
|
429 else
|
|
430 put_le32(pb, 0);
|
|
431 put_le32(pb, 0); /* reserved */
|
|
432 return 32;
|
|
433 }
|
|
434
|
|
435 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *track)
|
|
436 {
|
|
437 /* FIXME implement */
|
|
438 put_be32(pb, 0); /* drop frame flag */
|
|
439 put_be32(pb, 0); /* reserved */
|
|
440 put_be32(pb, 0); /* reserved */
|
|
441 put_be32(pb, 0); /* reserved */
|
|
442 put_be32(pb, 0); /* reserved */
|
|
443 put_be32(pb, 0); /* reserved */
|
|
444 put_be32(pb, 0); /* reserved */
|
|
445 put_be32(pb, 0); /* reserved */
|
|
446 return 32;
|
|
447 }
|
|
448
|
|
449 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *track)
|
|
450 {
|
|
451 int i;
|
|
452
|
|
453 for (i = 0; i < 8; i++) {
|
|
454 put_be32(pb, 0);
|
|
455 }
|
|
456 return 32;
|
|
457 }
|
|
458
|
|
459 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *track)
|
|
460 {
|
|
461 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
|
|
462 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
|
|
463 put_le32(pb, 0); /* number of fields over which to ramp up sound level */
|
|
464 put_le32(pb, 0); /* number of fields over which to ramp down sound level */
|
|
465 put_le32(pb, 0); /* reserved */
|
|
466 put_le32(pb, 0); /* reserved */
|
|
467 return 32;
|
|
468 }
|
|
469
|
|
470 #if 0
|
|
471 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track)
|
|
472 {
|
|
473 put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
|
|
474 put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
|
|
475 put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
|
|
476 put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
|
|
477 return 32;
|
|
478 }
|
|
479 #endif
|
|
480
|
|
481 static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
|
|
482 {
|
|
483 offset_t pos;
|
|
484 int i;
|
|
485
|
|
486 pos = url_ftell(pb);
|
|
487 ctx->umf_media_offset = pos - ctx->umf_start_offset;
|
|
488 for (i = 0; i < ctx->fc->nb_streams; ++i) {
|
|
489 GXFStreamContext *sc = &ctx->streams[i];
|
|
490 char buffer[88];
|
|
491 offset_t startpos, curpos;
|
|
492 int path_size = strlen(ES_NAME_PATTERN);
|
|
493
|
|
494 startpos = url_ftell(pb);
|
|
495 put_le16(pb, 0); /* length */
|
|
496 put_le16(pb, sc->media_info);
|
|
497 put_le16(pb, 0); /* reserved */
|
|
498 put_le16(pb, 0); /* reserved */
|
|
499 put_le32(pb, ctx->nb_frames);
|
|
500 put_le32(pb, 0); /* attributes rw, ro */
|
|
501 put_le32(pb, 0); /* mark in */
|
|
502 put_le32(pb, ctx->nb_frames); /* mark out */
|
|
503 strncpy(buffer, ES_NAME_PATTERN, path_size);
|
|
504 put_buffer(pb, (uint8_t *)buffer, path_size);
|
|
505 put_be16(pb, sc->media_info);
|
|
506 put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
|
|
507 put_le32(pb, sc->track_type);
|
|
508 put_le32(pb, sc->sample_rate);
|
|
509 put_le32(pb, sc->sample_size);
|
|
510 put_le32(pb, 0); /* reserved */
|
|
511 switch (sc->codec->codec_id) {
|
|
512 case CODEC_ID_MPEG2VIDEO:
|
|
513 gxf_write_umf_media_mpeg(pb, sc);
|
|
514 break;
|
|
515 case CODEC_ID_PCM_S16LE:
|
|
516 gxf_write_umf_media_audio(pb, sc);
|
|
517 break;
|
|
518 case CODEC_ID_DVVIDEO:
|
|
519 gxf_write_umf_media_dv(pb, sc);
|
|
520 break;
|
|
521 default:
|
|
522 gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
|
|
523 }
|
|
524 curpos = url_ftell(pb);
|
|
525 url_fseek(pb, startpos, SEEK_SET);
|
|
526 put_le16(pb, curpos - startpos);
|
|
527 url_fseek(pb, curpos, SEEK_SET);
|
|
528 }
|
|
529 return url_ftell(pb) - pos;
|
|
530 }
|
|
531
|
|
532 static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
|
|
533 {
|
|
534 offset_t pos = url_ftell(pb);
|
|
535 ctx->umf_user_data_offset = pos - ctx->umf_start_offset;
|
|
536 put_le32(pb, 20);
|
|
537 put_le32(pb, 0);
|
|
538 put_le16(pb, 0);
|
|
539 put_le16(pb, 0);
|
|
540 put_le32(pb, 0);
|
|
541 put_byte(pb, 0);
|
|
542 put_byte(pb, 0);
|
|
543 put_byte(pb, 0);
|
|
544 put_byte(pb, 0);
|
|
545 return 20;
|
|
546 }
|
|
547
|
|
548 static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
|
|
549 {
|
|
550 offset_t pos = url_ftell(pb);
|
|
551
|
|
552 gxf_write_packet_header(pb, PKT_UMF);
|
|
553
|
|
554 /* preamble */
|
|
555 put_byte(pb, 3); /* first and last (only) packet */
|
|
556 put_be32(pb, ctx->umf_length); /* data length */
|
|
557
|
|
558 ctx->umf_start_offset = url_ftell(pb);
|
|
559 gxf_write_umf_payload(pb, ctx);
|
|
560 gxf_write_umf_material_description(pb, ctx);
|
|
561 ctx->umf_track_size = gxf_write_umf_track_description(pb, ctx);
|
|
562 ctx->umf_media_size = gxf_write_umf_media_description(pb, ctx);
|
|
563 ctx->umf_user_data_size = gxf_write_umf_user_data(pb, ctx);
|
|
564 ctx->umf_length = url_ftell(pb) - ctx->umf_start_offset;
|
|
565 return updatePacketSize(pb, pos);
|
|
566 }
|
|
567
|
|
568 static int gxf_write_header(AVFormatContext *s)
|
|
569 {
|
|
570 ByteIOContext *pb = &s->pb;
|
|
571 GXFContext *gxf = s->priv_data;
|
|
572 int i;
|
|
573
|
|
574 gxf->fc = s;
|
|
575 gxf->flags |= 0x00080000; /* material is simple clip */
|
|
576 for (i = 0; i < s->nb_streams; ++i) {
|
|
577 AVStream *st = s->streams[i];
|
|
578 GXFStreamContext *sc = &gxf->streams[i];
|
|
579
|
|
580 sc->codec = st->codec;
|
|
581 sc->index = i;
|
|
582 sc->media_type = codec_get_tag(gxf_media_types, sc->codec->codec_id);
|
|
583 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
|
|
584 if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
|
|
585 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
|
|
586 return -1;
|
|
587 }
|
|
588 if (st->codec->sample_rate != 48000) {
|
|
589 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
|
|
590 return -1;
|
|
591 }
|
|
592 if (st->codec->channels != 1) {
|
|
593 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
|
|
594 return -1;
|
|
595 }
|
|
596 sc->track_type = 2;
|
|
597 sc->sample_rate = st->codec->sample_rate;
|
|
598 sc->sample_size = 16;
|
|
599 sc->frame_rate_index = -2;
|
|
600 sc->lines_index = -2;
|
|
601 sc->fields = -2;
|
|
602 gxf->audio_tracks++;
|
|
603 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
|
|
604 fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
|
|
605 } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) {
|
|
606 /* FIXME check from time_base ? */
|
|
607 if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */
|
|
608 sc->frame_rate_index = 5;
|
|
609 sc->sample_rate = 60;
|
|
610 gxf->flags |= 0x00000080;
|
|
611 } else { /* assume PAL */
|
|
612 sc->frame_rate_index = 6;
|
|
613 sc->media_type++;
|
|
614 sc->sample_rate = 50;
|
|
615 gxf->flags |= 0x00000040;
|
|
616 }
|
|
617 gxf->sample_rate = sc->sample_rate;
|
|
618 if (gxf_find_lines_index(sc) < 0)
|
|
619 sc->lines_index = -1;
|
|
620 sc->sample_size = st->codec->bit_rate;
|
|
621 sc->fields = 2; /* interlaced */
|
|
622 switch (sc->codec->codec_id) {
|
|
623 case CODEC_ID_MPEG2VIDEO:
|
|
624 sc->track_type = 4;
|
|
625 gxf->mpeg_tracks++;
|
|
626 gxf->flags |= 0x00008000;
|
|
627 break;
|
|
628 case CODEC_ID_DVVIDEO:
|
|
629 if (sc->codec->pix_fmt == PIX_FMT_YUV422P) {
|
|
630 sc->media_type += 2;
|
|
631 sc->track_type = 6;
|
|
632 gxf->flags |= 0x00002000;
|
|
633 } else {
|
|
634 sc->track_type = 5;
|
|
635 gxf->flags |= 0x00001000;
|
|
636 }
|
|
637 break;
|
|
638 default:
|
|
639 av_log(NULL, AV_LOG_ERROR, "video codec not supported\n");
|
|
640 return -1;
|
|
641 }
|
|
642 }
|
|
643 }
|
|
644 gxf_write_map_packet(pb, gxf);
|
|
645 //gxf_write_flt_packet(pb, gxf);
|
|
646 gxf_write_umf_packet(pb, gxf);
|
|
647 put_flush_packet(pb);
|
|
648 return 0;
|
|
649 }
|
|
650
|
|
651 static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
|
|
652 {
|
|
653 offset_t pos = url_ftell(pb);
|
|
654
|
|
655 gxf_write_packet_header(pb, PKT_EOS);
|
|
656 return updatePacketSize(pb, pos);
|
|
657 }
|
|
658
|
|
659 static int gxf_write_trailer(AVFormatContext *s)
|
|
660 {
|
|
661 ByteIOContext *pb = &s->pb;
|
|
662 GXFContext *gxf = s->priv_data;
|
|
663 offset_t end;
|
|
664 int i;
|
|
665
|
|
666 for (i = 0; i < s->nb_streams; ++i) {
|
|
667 if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
|
|
668 fifo_free(&gxf->streams[i].audio_buffer);
|
|
669 }
|
|
670 if (s->streams[i]->codec->frame_number > gxf->nb_frames)
|
|
671 gxf->nb_frames = 2 * s->streams[i]->codec->frame_number;
|
|
672 }
|
|
673
|
|
674 gxf_write_eos_packet(pb, gxf);
|
|
675 end = url_ftell(pb);
|
|
676 url_fseek(pb, 0, SEEK_SET);
|
|
677 /* overwrite map and umf packets with new values */
|
|
678 gxf_write_map_packet(pb, gxf);
|
|
679 //gxf_write_flt_packet(pb, gxf);
|
|
680 gxf_write_umf_packet(pb, gxf);
|
|
681 url_fseek(pb, end, SEEK_SET);
|
|
682 return 0;
|
|
683 }
|
|
684
|
|
685 static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size)
|
|
686 {
|
|
687 GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
|
|
688
|
|
689 put_byte(pb, sc->media_type);
|
|
690 put_byte(pb, sc->index);
|
|
691 put_be32(pb, ctx->field_number);
|
|
692 if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
|
|
693 put_be16(pb, 0);
|
|
694 put_be16(pb, size / 2);
|
|
695 } else if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
|
|
696 if (pkt->flags & PKT_FLAG_KEY) {
|
|
697 put_byte(pb, 0x0d);
|
|
698 sc->iframes++;
|
|
699 } else if (pkt->pts != pkt->dts) {
|
|
700 put_byte(pb, 0x0f);
|
|
701 sc->bframes++;
|
|
702 } else {
|
|
703 put_byte(pb, 0x0e);
|
|
704 sc->pframes++;
|
|
705 }
|
|
706 put_be24(pb, size);
|
|
707 } else if (sc->codec->codec_id == CODEC_ID_DVVIDEO) {
|
|
708 put_byte(pb, size / 4096);
|
|
709 put_be24(pb, 0);
|
|
710 } else
|
|
711 put_be32(pb, size);
|
|
712 put_be32(pb, ctx->field_number);
|
|
713 put_byte(pb, 1); /* flags */
|
|
714 put_byte(pb, 0); /* reserved */
|
|
715 return 16;
|
|
716 }
|
|
717
|
|
718 static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt)
|
|
719 {
|
|
720 GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
|
|
721 offset_t pos = url_ftell(pb);
|
|
722 int padding = 0;
|
|
723
|
|
724 gxf_write_packet_header(pb, PKT_MEDIA);
|
|
725 if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
|
|
726 padding = 4 - pkt->size % 4;
|
1212
|
727 else if (sc->codec->codec_type == CODEC_TYPE_AUDIO)
|
|
728 padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
|
1183
|
729 gxf_write_media_preamble(pb, ctx, pkt, pkt->size + padding);
|
1212
|
730 put_buffer(pb, pkt->data, pkt->size);
|
|
731 gxf_write_padding(pb, padding);
|
|
732 if (sc->codec->codec_type == CODEC_TYPE_VIDEO)
|
1183
|
733 ctx->field_number += 2;
|
|
734 return updatePacketSize(pb, pos);
|
|
735 }
|
|
736
|
|
737 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
738 {
|
|
739 GXFContext *gxf = s->priv_data;
|
|
740
|
|
741 gxf_write_media_packet(&s->pb, gxf, pkt);
|
|
742 put_flush_packet(&s->pb);
|
|
743 return 0;
|
|
744 }
|
|
745
|
|
746 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
|
|
747 {
|
|
748 AVPacketList *pktl, **next_point, *this_pktl;
|
|
749 GXFContext *gxf = s->priv_data;
|
|
750 GXFStreamContext *sc;
|
|
751 int i;
|
|
752
|
|
753 if (pkt) {
|
|
754 sc = &gxf->streams[pkt->stream_index];
|
|
755 if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
|
|
756 fifo_write(&sc->audio_buffer, pkt->data, pkt->size, NULL);
|
|
757 } else {
|
|
758 this_pktl = av_mallocz(sizeof(AVPacketList));
|
|
759 this_pktl->pkt = *pkt;
|
|
760 if(pkt->destruct == av_destruct_packet)
|
|
761 pkt->destruct = NULL; // non shared -> must keep original from being freed
|
|
762 else
|
|
763 av_dup_packet(&this_pktl->pkt); //shared -> must dup
|
|
764 next_point = &s->packet_buffer;
|
|
765 while(*next_point){
|
|
766 AVStream *st= s->streams[ pkt->stream_index];
|
|
767 AVStream *st2= s->streams[ (*next_point)->pkt.stream_index];
|
|
768 int64_t left= st2->time_base.num * (int64_t)st ->time_base.den;
|
|
769 int64_t right= st ->time_base.num * (int64_t)st2->time_base.den;
|
|
770 if((*next_point)->pkt.dts * left > pkt->dts * right) //FIXME this can overflow
|
|
771 break;
|
|
772 next_point= &(*next_point)->next;
|
|
773 }
|
|
774 this_pktl->next = *next_point;
|
|
775 *next_point = this_pktl;
|
|
776 }
|
|
777 }
|
|
778
|
|
779 if (gxf->audio_written == gxf->audio_tracks) {
|
|
780 if (!s->packet_buffer) {
|
|
781 gxf->audio_written = 0;
|
|
782 return 0;
|
|
783 }
|
|
784 pktl = s->packet_buffer;
|
|
785 *out = pktl->pkt;
|
|
786 s->packet_buffer = pktl->next;
|
|
787 av_freep(&pktl);
|
|
788 return 1;
|
|
789 } else {
|
|
790 for (i = 0; i < s->nb_streams; i++) {
|
|
791 sc = &gxf->streams[i];
|
|
792 if (sc->codec->codec_type == CODEC_TYPE_AUDIO &&
|
|
793 (flush || fifo_size(&sc->audio_buffer, NULL) >= GXF_AUDIO_PACKET_SIZE)) {
|
|
794 int size = flush ? fifo_size(&sc->audio_buffer, NULL) : GXF_AUDIO_PACKET_SIZE;
|
1212
|
795 av_new_packet(out, size);
|
1183
|
796 fifo_read(&sc->audio_buffer, out->data, size, NULL);
|
|
797 gxf->audio_written++;
|
|
798 out->stream_index = i;
|
|
799 return 1;
|
|
800 }
|
|
801 }
|
|
802 }
|
|
803 av_init_packet(out);
|
|
804 return 0;
|
|
805 }
|
|
806
|
|
807 AVOutputFormat gxf_muxer = {
|
|
808 "gxf",
|
|
809 "GXF format",
|
|
810 NULL,
|
|
811 "gxf",
|
|
812 sizeof(GXFContext),
|
|
813 CODEC_ID_PCM_S16LE,
|
|
814 CODEC_ID_MPEG2VIDEO,
|
|
815 gxf_write_header,
|
|
816 gxf_write_packet,
|
|
817 gxf_write_trailer,
|
|
818 0,
|
|
819 NULL,
|
|
820 gxf_interleave_packet,
|
|
821 };
|