]> git.sesse.net Git - ffmpeg/blob - libavformat/gxfenc.c
a6d2ef3338aab93857b4371ba79cb27c2244e53d
[ffmpeg] / 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 file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/fifo.h"
23 #include "avformat.h"
24 #include "gxf.h"
25 #include "riff.h"
26 #include "audiointerleave.h"
27
28 #define GXF_AUDIO_PACKET_SIZE 65536
29
30 typedef struct GXFStreamContext {
31     AudioInterleaveContext aic;
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;
44     int b_per_i_or_p; ///< number of B frames per I frame or P frame
45     int first_gop_closed;
46     unsigned order;   ///< interleaving order
47 } GXFStreamContext;
48
49 typedef struct GXFContext {
50     uint32_t nb_fields;
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;
60     AVRational time_base;
61     int flags;
62     GXFStreamContext timecode_track;
63     unsigned *flt_entries;    ///< offsets of packets /1024, starts after 2nd video field
64     unsigned flt_entries_nb;
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 AVCodecTag 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(AVStream *st)
106 {
107     GXFStreamContext *sc = st->priv_data;
108     int i;
109
110     for (i = 0; i < 6; ++i) {
111         if (st->codec->height == gxf_lines_tab[i].height) {
112             sc->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, int64_t to_pad)
120 {
121     for (; to_pad > 0; to_pad--) {
122         put_byte(pb, 0);
123     }
124 }
125
126 static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos)
127 {
128     int64_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 int64_t updateSize(ByteIOContext *pb, int64_t pos)
144 {
145     int64_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, GXFPktType 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, AVStream *st)
166 {
167     GXFStreamContext *sc = st->priv_data;
168     char buffer[1024];
169     int size, starting_line;
170
171     if (sc->iframes) {
172         sc->p_per_gop = sc->pframes / sc->iframes;
173         if (sc->pframes % sc->iframes)
174             sc->p_per_gop++;
175         if (sc->pframes) {
176             sc->b_per_i_or_p = sc->bframes / sc->pframes;
177             if (sc->bframes % sc->pframes)
178                 sc->b_per_i_or_p++;
179         }
180         if (sc->p_per_gop > 9)
181             sc->p_per_gop = 9; /* ensure value won't take more than one char */
182         if (sc->b_per_i_or_p > 9)
183             sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */
184     }
185     if (st->codec->height == 512 || st->codec->height == 608)
186         starting_line = 7; // VBI
187     else if (st->codec->height == 480)
188         starting_line = 20;
189     else
190         starting_line = 23; // default PAL
191
192     size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
193                     "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
194                     (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
195                     st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
196                     starting_line, st->codec->height / 16);
197     put_byte(pb, TRACK_MPG_AUX);
198     put_byte(pb, size + 1);
199     put_buffer(pb, (uint8_t *)buffer, size + 1);
200     return size + 3;
201 }
202
203 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *sc)
204 {
205     put_byte(pb, 0); /* fields */
206     put_byte(pb, 0);  /* seconds */
207     put_byte(pb, 0); /* minutes */
208     put_byte(pb, 0); /* flags + hours */
209     /* reserved */
210     put_be32(pb, 0);
211     return 8;
212 }
213
214 static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
215 {
216     ByteIOContext *pb = s->pb;
217     int64_t pos;
218     int mpeg = sc->track_type == 4 || sc->track_type == 9;
219
220     /* track description section */
221     put_byte(pb, sc->media_type + 0x80);
222     put_byte(pb, index + 0xC0);
223
224     pos = url_ftell(pb);
225     put_be16(pb, 0); /* size */
226
227     /* media file name */
228     put_byte(pb, TRACK_NAME);
229     put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
230     put_tag(pb, ES_NAME_PATTERN);
231     put_be16(pb, sc->media_info);
232     put_byte(pb, 0);
233
234     if (!mpeg) {
235         /* auxiliary information */
236         put_byte(pb, TRACK_AUX);
237         put_byte(pb, 8);
238         if (sc->track_type == 3)
239             gxf_write_timecode_auxiliary(pb, sc);
240         else
241             put_le64(pb, 0);
242     }
243
244     /* file system version */
245     put_byte(pb, TRACK_VER);
246     put_byte(pb, 4);
247     put_be32(pb, 0);
248
249     if (mpeg)
250         gxf_write_mpeg_auxiliary(pb, s->streams[index]);
251
252     /* frame rate */
253     put_byte(pb, TRACK_FPS);
254     put_byte(pb, 4);
255     put_be32(pb, sc->frame_rate_index);
256
257     /* lines per frame */
258     put_byte(pb, TRACK_LINES);
259     put_byte(pb, 4);
260     put_be32(pb, sc->lines_index);
261
262     /* fields per frame */
263     put_byte(pb, TRACK_FPF);
264     put_byte(pb, 4);
265     put_be32(pb, sc->fields);
266
267     return updateSize(pb, pos);
268 }
269
270 static int gxf_write_material_data_section(AVFormatContext *s)
271 {
272     GXFContext *gxf = s->priv_data;
273     ByteIOContext *pb = s->pb;
274     int64_t pos;
275     const char *filename = strrchr(s->filename, '/');
276
277     pos = url_ftell(pb);
278     put_be16(pb, 0); /* size */
279
280     /* name */
281     if (filename)
282         filename++;
283     else
284         filename = s->filename;
285     put_byte(pb, MAT_NAME);
286     put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
287     put_tag(pb, SERVER_PATH);
288     put_tag(pb, filename);
289     put_byte(pb, 0);
290
291     /* first field */
292     put_byte(pb, MAT_FIRST_FIELD);
293     put_byte(pb, 4);
294     put_be32(pb, 0);
295
296     /* last field */
297     put_byte(pb, MAT_LAST_FIELD);
298     put_byte(pb, 4);
299     put_be32(pb, gxf->nb_fields);
300
301     /* reserved */
302     put_byte(pb, MAT_MARK_IN);
303     put_byte(pb, 4);
304     put_be32(pb, 0);
305
306     put_byte(pb, MAT_MARK_OUT);
307     put_byte(pb, 4);
308     put_be32(pb, gxf->nb_fields);
309
310     /* estimated size */
311     put_byte(pb, MAT_SIZE);
312     put_byte(pb, 4);
313     put_be32(pb, url_fsize(pb) / 1024);
314
315     return updateSize(pb, pos);
316 }
317
318 static int gxf_write_track_description_section(AVFormatContext *s)
319 {
320     GXFContext *gxf = s->priv_data;
321     ByteIOContext *pb = s->pb;
322     int64_t pos;
323     int i;
324
325     pos = url_ftell(pb);
326     put_be16(pb, 0); /* size */
327     for (i = 0; i < s->nb_streams; ++i)
328         gxf_write_track_description(s, s->streams[i]->priv_data, i);
329
330     gxf_write_track_description(s, &gxf->timecode_track, s->nb_streams);
331
332     return updateSize(pb, pos);
333 }
334
335 static int gxf_write_map_packet(AVFormatContext *s)
336 {
337     ByteIOContext *pb = s->pb;
338     int64_t pos = url_ftell(pb);
339
340     gxf_write_packet_header(pb, PKT_MAP);
341
342     /* preamble */
343     put_byte(pb, 0xE0); /* version */
344     put_byte(pb, 0xFF); /* reserved */
345
346     gxf_write_material_data_section(s);
347     gxf_write_track_description_section(s);
348
349     return updatePacketSize(pb, pos);
350 }
351
352 static int gxf_write_flt_packet(AVFormatContext *s)
353 {
354     GXFContext *gxf = s->priv_data;
355     ByteIOContext *pb = s->pb;
356     int64_t pos = url_ftell(pb);
357     int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
358     int flt_entries = gxf->nb_fields / fields_per_flt - 1;
359     int i = 0;
360
361     gxf_write_packet_header(pb, PKT_FLT);
362
363     put_le32(pb, fields_per_flt); /* number of fields */
364     put_le32(pb, flt_entries); /* number of active flt entries */
365
366     if (gxf->flt_entries) {
367         for (i = 0; i < flt_entries; i++)
368             put_le32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
369     }
370
371     for (; i < 1000; i++)
372         put_le32(pb, 0);
373
374     return updatePacketSize(pb, pos);
375 }
376
377 static int gxf_write_umf_material_description(AVFormatContext *s)
378 {
379     GXFContext *gxf = s->priv_data;
380     ByteIOContext *pb = s->pb;
381     int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
382
383     // XXX drop frame
384     uint32_t timecode =
385         gxf->nb_fields / (timecode_base * 3600) % 24 << 24 | // hours
386         gxf->nb_fields / (timecode_base * 60) % 60   << 16 | // minutes
387         gxf->nb_fields /  timecode_base % 60         <<  8 | // seconds
388         gxf->nb_fields %  timecode_base;                     // fields
389
390     put_le32(pb, gxf->flags);
391     put_le32(pb, gxf->nb_fields); /* length of the longest track */
392     put_le32(pb, gxf->nb_fields); /* length of the shortest track */
393     put_le32(pb, 0); /* mark in */
394     put_le32(pb, gxf->nb_fields); /* mark out */
395     put_le32(pb, 0); /* timecode mark in */
396     put_le32(pb, timecode); /* timecode mark out */
397     put_le64(pb, s->timestamp); /* modification time */
398     put_le64(pb, s->timestamp); /* creation time */
399     put_le16(pb, 0); /* reserved */
400     put_le16(pb, 0); /* reserved */
401     put_le16(pb, gxf->audio_tracks);
402     put_le16(pb, 1); /* timecode track count */
403     put_le16(pb, 0); /* reserved */
404     put_le16(pb, gxf->mpeg_tracks);
405     return 48;
406 }
407
408 static int gxf_write_umf_payload(AVFormatContext *s)
409 {
410     GXFContext *gxf = s->priv_data;
411     ByteIOContext *pb = s->pb;
412
413     put_le32(pb, gxf->umf_length); /* total length of the umf data */
414     put_le32(pb, 3); /* version */
415     put_le32(pb, s->nb_streams+1);
416     put_le32(pb, gxf->umf_track_offset); /* umf track section offset */
417     put_le32(pb, gxf->umf_track_size);
418     put_le32(pb, s->nb_streams+1);
419     put_le32(pb, gxf->umf_media_offset);
420     put_le32(pb, gxf->umf_media_size);
421     put_le32(pb, gxf->umf_length); /* user data offset */
422     put_le32(pb, 0); /* user data size */
423     put_le32(pb, 0); /* reserved */
424     put_le32(pb, 0); /* reserved */
425     return 48;
426 }
427
428 static int gxf_write_umf_track_description(AVFormatContext *s)
429 {
430     ByteIOContext *pb = s->pb;
431     GXFContext *gxf = s->priv_data;
432     int64_t pos = url_ftell(pb);
433     int i;
434
435     gxf->umf_track_offset = pos - gxf->umf_start_offset;
436     for (i = 0; i < s->nb_streams; ++i) {
437         GXFStreamContext *sc = s->streams[i]->priv_data;
438         put_le16(pb, sc->media_info);
439         put_le16(pb, 1);
440     }
441
442     put_le16(pb, gxf->timecode_track.media_info);
443     put_le16(pb, 1);
444
445     return url_ftell(pb) - pos;
446 }
447
448 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, AVStream *st)
449 {
450     GXFStreamContext *sc = st->priv_data;
451
452     if (st->codec->pix_fmt == PIX_FMT_YUV422P)
453         put_le32(pb, 2);
454     else
455         put_le32(pb, 1); /* default to 420 */
456     put_le32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
457     put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
458     put_le32(pb, 1); /* I picture per GOP */
459     put_le32(pb, sc->p_per_gop);
460     put_le32(pb, sc->b_per_i_or_p);
461     if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
462         put_le32(pb, 2);
463     else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO)
464         put_le32(pb, 1);
465     else
466         put_le32(pb, 0);
467     put_le32(pb, 0); /* reserved */
468     return 32;
469 }
470
471 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *sc)
472 {
473     put_le32(pb, 1); /* non drop frame */
474     put_le32(pb, 0); /* reserved */
475     put_le32(pb, 0); /* reserved */
476     put_le32(pb, 0); /* reserved */
477     put_le32(pb, 0); /* reserved */
478     put_le32(pb, 0); /* reserved */
479     put_le32(pb, 0); /* reserved */
480     put_le32(pb, 0); /* reserved */
481     return 32;
482 }
483
484 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *sc)
485 {
486     int i;
487
488     for (i = 0; i < 8; i++) {
489         put_be32(pb, 0);
490     }
491     return 32;
492 }
493
494 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *sc)
495 {
496     put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
497     put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
498     put_le32(pb, 0); /* number of fields over which to ramp up sound level */
499     put_le32(pb, 0); /* number of fields over which to ramp down sound level */
500     put_le32(pb, 0); /* reserved */
501     put_le32(pb, 0); /* reserved */
502     return 32;
503 }
504
505 #if 0
506 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *sc)
507 {
508     put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
509     put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
510     put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
511     put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
512     return 32;
513 }
514 #endif
515
516 static int gxf_write_umf_media_description(AVFormatContext *s)
517 {
518     GXFContext *gxf = s->priv_data;
519     ByteIOContext *pb = s->pb;
520     int64_t pos;
521     int i;
522
523     pos = url_ftell(pb);
524     gxf->umf_media_offset = pos - gxf->umf_start_offset;
525     for (i = 0; i <= s->nb_streams; ++i) {
526         GXFStreamContext *sc;
527         char buffer[88];
528         int64_t startpos, curpos;
529         int path_size = strlen(ES_NAME_PATTERN);
530
531         if (i == s->nb_streams)
532             sc = &gxf->timecode_track;
533         else
534             sc = s->streams[i]->priv_data;
535
536         memset(buffer, 0, 88);
537         startpos = url_ftell(pb);
538         put_le16(pb, 0); /* length */
539         put_le16(pb, sc->media_info);
540         put_le16(pb, 0); /* reserved */
541         put_le16(pb, 0); /* reserved */
542         put_le32(pb, gxf->nb_fields);
543         put_le32(pb, 0); /* attributes rw, ro */
544         put_le32(pb, 0); /* mark in */
545         put_le32(pb, gxf->nb_fields); /* mark out */
546         strncpy(buffer, ES_NAME_PATTERN, path_size);
547         put_buffer(pb, (uint8_t *)buffer, path_size);
548         put_be16(pb, sc->media_info);
549         put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
550         put_le32(pb, sc->track_type);
551         put_le32(pb, sc->sample_rate);
552         put_le32(pb, sc->sample_size);
553         put_le32(pb, 0); /* reserved */
554
555         if (sc == &gxf->timecode_track)
556             gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
557         else {
558             AVStream *st = s->streams[i];
559         switch (st->codec->codec_id) {
560         case CODEC_ID_MPEG2VIDEO:
561             gxf_write_umf_media_mpeg(pb, st);
562             break;
563         case CODEC_ID_PCM_S16LE:
564             gxf_write_umf_media_audio(pb, sc);
565             break;
566         case CODEC_ID_DVVIDEO:
567             gxf_write_umf_media_dv(pb, sc);
568             break;
569         }
570         }
571
572         curpos = url_ftell(pb);
573         url_fseek(pb, startpos, SEEK_SET);
574         put_le16(pb, curpos - startpos);
575         url_fseek(pb, curpos, SEEK_SET);
576     }
577     return url_ftell(pb) - pos;
578 }
579
580 static int gxf_write_umf_packet(AVFormatContext *s)
581 {
582     GXFContext *gxf = s->priv_data;
583     ByteIOContext *pb = s->pb;
584     int64_t pos = url_ftell(pb);
585
586     gxf_write_packet_header(pb, PKT_UMF);
587
588     /* preamble */
589     put_byte(pb, 3); /* first and last (only) packet */
590     put_be32(pb, gxf->umf_length); /* data length */
591
592     gxf->umf_start_offset = url_ftell(pb);
593     gxf_write_umf_payload(s);
594     gxf_write_umf_material_description(s);
595     gxf->umf_track_size = gxf_write_umf_track_description(s);
596     gxf->umf_media_size = gxf_write_umf_media_description(s);
597     gxf->umf_length = url_ftell(pb) - gxf->umf_start_offset;
598     return updatePacketSize(pb, pos);
599 }
600
601 static const int GXF_samples_per_frame[] = { 32768, 0 };
602
603 static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
604 {
605     if (!vsc)
606         return;
607
608     sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
609     sc->sample_rate = vsc->sample_rate;
610     sc->media_info = ('T'<<8) | '0';
611     sc->track_type = 3;
612     sc->frame_rate_index = vsc->frame_rate_index;
613     sc->lines_index = vsc->lines_index;
614     sc->sample_size = 16;
615     sc->fields = vsc->fields;
616 }
617
618 static int gxf_write_header(AVFormatContext *s)
619 {
620     ByteIOContext *pb = s->pb;
621     GXFContext *gxf = s->priv_data;
622     GXFStreamContext *vsc = NULL;
623     uint8_t tracks[255] = {0};
624     int i, media_info = 0;
625
626     gxf->flags |= 0x00080000; /* material is simple clip */
627     for (i = 0; i < s->nb_streams; ++i) {
628         AVStream *st = s->streams[i];
629         GXFStreamContext *sc = av_mallocz(sizeof(*sc));
630         if (!sc)
631             return AVERROR(ENOMEM);
632         st->priv_data = sc;
633
634         sc->media_type = codec_get_tag(gxf_media_types, st->codec->codec_id);
635         if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
636             if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
637                 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
638                 return -1;
639             }
640             if (st->codec->sample_rate != 48000) {
641                 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
642                 return -1;
643             }
644             if (st->codec->channels != 1) {
645                 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
646                 return -1;
647             }
648             sc->track_type = 2;
649             sc->sample_rate = st->codec->sample_rate;
650             av_set_pts_info(st, 64, 1, sc->sample_rate);
651             sc->sample_size = 16;
652             sc->frame_rate_index = -2;
653             sc->lines_index = -2;
654             sc->fields = -2;
655             gxf->audio_tracks++;
656             gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
657             media_info = 'A';
658         } else if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
659             if (i != 0) {
660                 av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
661                 return -1;
662             }
663             /* FIXME check from time_base ? */
664             if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
665                 sc->frame_rate_index = 5;
666                 sc->sample_rate = 60;
667                 gxf->flags |= 0x00000080;
668                 gxf->time_base = (AVRational){ 1001, 60000 };
669             } else { /* assume PAL */
670                 sc->frame_rate_index = 6;
671                 sc->media_type++;
672                 sc->sample_rate = 50;
673                 gxf->flags |= 0x00000040;
674                 gxf->time_base = (AVRational){ 1, 50 };
675             }
676             av_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
677             if (gxf_find_lines_index(st) < 0)
678                 sc->lines_index = -1;
679             sc->sample_size = st->codec->bit_rate;
680             sc->fields = 2; /* interlaced */
681
682             vsc = sc;
683
684             switch (st->codec->codec_id) {
685             case CODEC_ID_MJPEG:
686                 sc->track_type = 1;
687                 gxf->flags |= 0x00004000;
688                 media_info = 'J';
689                 break;
690             case CODEC_ID_MPEG1VIDEO:
691                 sc->track_type = 9;
692                 gxf->mpeg_tracks++;
693                 media_info = 'L';
694                 break;
695             case CODEC_ID_MPEG2VIDEO:
696                 sc->first_gop_closed = -1;
697                 sc->track_type = 4;
698                 gxf->mpeg_tracks++;
699                 gxf->flags |= 0x00008000;
700                 media_info = 'M';
701                 break;
702             case CODEC_ID_DVVIDEO:
703                 if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
704                     sc->media_type += 2;
705                     sc->track_type = 6;
706                     gxf->flags |= 0x00002000;
707                     media_info = 'E';
708                 } else {
709                     sc->track_type = 5;
710                     gxf->flags |= 0x00001000;
711                     media_info = 'D';
712                 }
713                 break;
714             default:
715                 av_log(s, AV_LOG_ERROR, "video codec not supported\n");
716                 return -1;
717             }
718         }
719         /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
720         sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
721         sc->order = s->nb_streams - st->index;
722     }
723
724     if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
725         return -1;
726
727     gxf_init_timecode_track(&gxf->timecode_track, vsc);
728     gxf->flags |= 0x200000; // time code track is non-drop frame
729
730     gxf_write_map_packet(s);
731     gxf_write_flt_packet(s);
732     gxf_write_umf_packet(s);
733     put_flush_packet(pb);
734     return 0;
735 }
736
737 static int gxf_write_eos_packet(ByteIOContext *pb)
738 {
739     int64_t pos = url_ftell(pb);
740
741     gxf_write_packet_header(pb, PKT_EOS);
742     return updatePacketSize(pb, pos);
743 }
744
745 static int gxf_write_trailer(AVFormatContext *s)
746 {
747     GXFContext *gxf = s->priv_data;
748     ByteIOContext *pb = s->pb;
749     int64_t end;
750
751     ff_audio_interleave_close(s);
752
753     gxf_write_eos_packet(pb);
754     end = url_ftell(pb);
755     url_fseek(pb, 0, SEEK_SET);
756     /* overwrite map, flt and umf packets with new values */
757     gxf_write_map_packet(s);
758     gxf_write_flt_packet(s);
759     gxf_write_umf_packet(s);
760     url_fseek(pb, end, SEEK_SET);
761
762     av_freep(&gxf->flt_entries);
763
764     return 0;
765 }
766
767 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
768 {
769     uint32_t c=-1;
770     int i;
771     for(i=0; i<size-4 && c!=0x100; i++){
772         c = (c<<8) + buf[i];
773         if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
774             sc->first_gop_closed= (buf[i+4]>>6)&1;
775     }
776     return (buf[i+1]>>3)&7;
777 }
778
779 static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
780 {
781     GXFContext *gxf = s->priv_data;
782     ByteIOContext *pb = s->pb;
783     AVStream *st = s->streams[pkt->stream_index];
784     GXFStreamContext *sc = st->priv_data;
785     unsigned field_nb;
786     /* If the video is frame-encoded, the frame numbers shall be represented by
787      * even field numbers.
788      * see SMPTE360M-2004  6.4.2.1.3 Media field number */
789     if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
790         field_nb = gxf->nb_fields;
791     } else {
792         field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
793                                   (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
794     }
795
796     put_byte(pb, sc->media_type);
797     put_byte(pb, st->index);
798     put_be32(pb, field_nb);
799     if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
800         put_be16(pb, 0);
801         put_be16(pb, size / 2);
802     } else if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
803         int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
804         if (frame_type == FF_I_TYPE) {
805             put_byte(pb, 0x0d);
806             sc->iframes++;
807         } else if (frame_type == FF_B_TYPE) {
808             put_byte(pb, 0x0f);
809             sc->bframes++;
810         } else {
811             put_byte(pb, 0x0e);
812             sc->pframes++;
813         }
814         put_be24(pb, size);
815     } else if (st->codec->codec_id == CODEC_ID_DVVIDEO) {
816         put_byte(pb, size / 4096);
817         put_be24(pb, 0);
818     } else
819         put_be32(pb, size);
820     put_be32(pb, field_nb);
821     put_byte(pb, 1); /* flags */
822     put_byte(pb, 0); /* reserved */
823     return 16;
824 }
825
826 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
827 {
828     GXFContext *gxf = s->priv_data;
829     ByteIOContext *pb = s->pb;
830     AVStream *st = s->streams[pkt->stream_index];
831     int64_t pos = url_ftell(pb);
832     int padding = 0;
833
834     gxf_write_packet_header(pb, PKT_MEDIA);
835     if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
836         padding = 4 - pkt->size % 4;
837     else if (st->codec->codec_type == CODEC_TYPE_AUDIO)
838         padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
839     gxf_write_media_preamble(s, pkt, pkt->size + padding);
840     put_buffer(pb, pkt->data, pkt->size);
841     gxf_write_padding(pb, padding);
842
843     if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
844         if (!(gxf->flt_entries_nb % 500)) {
845             gxf->flt_entries = av_realloc(gxf->flt_entries,
846                                           (gxf->flt_entries_nb+500)*sizeof(*gxf->flt_entries));
847             if (!gxf->flt_entries) {
848                 av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
849                 return -1;
850             }
851         }
852         gxf->flt_entries[gxf->flt_entries_nb++] = url_ftell(pb) / 1024;
853         gxf->nb_fields += 2; // count fields
854     }
855
856     put_flush_packet(pb);
857
858     return updatePacketSize(pb, pos);
859 }
860
861 static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
862 {
863     GXFContext *gxf = s->priv_data;
864     AVPacket *pkt[2] = { cur, next };
865     int i, field_nb[2];
866     GXFStreamContext *sc[2];
867
868     for (i = 0; i < 2; i++) {
869         AVStream *st = s->streams[pkt[i]->stream_index];
870         sc[i] = st->priv_data;
871         if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
872             field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
873                                          (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
874             field_nb[i] &= ~1; // compare against even field number because audio must be before video
875         } else
876             field_nb[i] = pkt[i]->dts; // dts are field based
877     }
878
879     return field_nb[1] > field_nb[0] ||
880         (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
881 }
882
883 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
884 {
885     if (pkt && s->streams[pkt->stream_index]->codec->codec_type == CODEC_TYPE_VIDEO)
886         pkt->duration = 2; // enforce 2 fields
887     return ff_audio_rechunk_interleave(s, out, pkt, flush,
888                                av_interleave_packet_per_dts, gxf_compare_field_nb);
889 }
890
891 AVOutputFormat gxf_muxer = {
892     "gxf",
893     NULL_IF_CONFIG_SMALL("GXF format"),
894     NULL,
895     "gxf",
896     sizeof(GXFContext),
897     CODEC_ID_PCM_S16LE,
898     CODEC_ID_MPEG2VIDEO,
899     gxf_write_header,
900     gxf_write_packet,
901     gxf_write_trailer,
902     0,
903     NULL,
904     gxf_interleave_packet,
905 };