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