Statistics
| Branch: | Revision:

ffmpeg / libavformat / gxfenc.c @ a1b79792

History | View | Annotate | Download (28.7 KB)

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 "avformat.h"
23
#include "gxf.h"
24
#include "riff.h"
25
#include "audiointerleave.h"
26

    
27
#define GXF_AUDIO_PACKET_SIZE 65536
28

    
29
typedef struct GXFStreamContext {
30
    AudioInterleaveContext aic;
31
    uint32_t track_type;
32
    uint32_t sample_size;
33
    uint32_t sample_rate;
34
    uint16_t media_type;
35
    uint16_t media_info;
36
    int frame_rate_index;
37
    int lines_index;
38
    int fields;
39
    int iframes;
40
    int pframes;
41
    int bframes;
42
    int p_per_gop;
43
    int b_per_i_or_p; ///< number of B frames per I frame or P frame
44
    int first_gop_closed;
45
    unsigned order;   ///< interleaving order
46
} GXFStreamContext;
47

    
48
typedef struct GXFContext {
49
    uint32_t nb_fields;
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_length;
57
    uint16_t umf_track_size;
58
    uint16_t umf_media_size;
59
    AVRational time_base;
60
    int flags;
61
    GXFStreamContext timecode_track;
62
    unsigned *flt_entries;    ///< offsets of packets /1024, starts after 2nd video field
63
    unsigned flt_entries_nb;
64
    uint64_t *map_offsets;    ///< offset of map packets
65
    unsigned map_offsets_nb;
66
    unsigned packet_count;
67
} GXFContext;
68

    
69
static const struct {
70
    int height, index;
71
} gxf_lines_tab[] = {
72
    { 480,  1 }, /* NTSC */
73
    { 512,  1 }, /* NTSC + VBI */
74
    { 576,  2 }, /* PAL */
75
    { 608,  2 }, /* PAL + VBI */
76
    { 1080, 4 },
77
    { 720,  6 },
78
};
79

    
80
static const AVCodecTag gxf_media_types[] = {
81
    { CODEC_ID_MJPEG     ,   3 }, /* NTSC */
82
    { CODEC_ID_MJPEG     ,   4 }, /* PAL */
83
    { CODEC_ID_PCM_S24LE ,   9 },
84
    { CODEC_ID_PCM_S16LE ,  10 },
85
    { CODEC_ID_MPEG2VIDEO,  11 }, /* NTSC */
86
    { CODEC_ID_MPEG2VIDEO,  12 }, /* PAL */
87
    { CODEC_ID_DVVIDEO   ,  13 }, /* NTSC */
88
    { CODEC_ID_DVVIDEO   ,  14 }, /* PAL */
89
    { CODEC_ID_DVVIDEO   ,  15 }, /* 50M NTSC */
90
    { CODEC_ID_DVVIDEO   ,  16 }, /* 50M PAL */
91
    { CODEC_ID_AC3       ,  17 },
92
    //{ CODEC_ID_NONE,  ,   18 }, /* Non compressed 24 bit audio */
93
    { CODEC_ID_MPEG2VIDEO,  20 }, /* MPEG HD */
94
    { CODEC_ID_MPEG1VIDEO,  22 }, /* NTSC */
95
    { CODEC_ID_MPEG1VIDEO,  23 }, /* PAL */
96
    { CODEC_ID_NONE,         0 },
97
};
98

    
99
#define SERVER_PATH "EXT:/PDR/default/"
100
#define ES_NAME_PATTERN "EXT:/PDR/default/ES."
101

    
102
static int gxf_find_lines_index(AVStream *st)
103
{
104
    GXFStreamContext *sc = st->priv_data;
105
    int i;
106

    
107
    for (i = 0; i < 6; ++i) {
108
        if (st->codec->height == gxf_lines_tab[i].height) {
109
            sc->lines_index = gxf_lines_tab[i].index;
110
            return 0;
111
        }
112
    }
113
    return -1;
114
}
115

    
116
static void gxf_write_padding(AVIOContext *pb, int64_t to_pad)
117
{
118
    for (; to_pad > 0; to_pad--) {
119
        avio_w8(pb, 0);
120
    }
121
}
122

    
123
static int64_t updatePacketSize(AVIOContext *pb, int64_t pos)
124
{
125
    int64_t curpos;
126
    int size;
127

    
128
    size = url_ftell(pb) - pos;
129
    if (size % 4) {
130
        gxf_write_padding(pb, 4 - size % 4);
131
        size = url_ftell(pb) - pos;
132
    }
133
    curpos = url_ftell(pb);
134
    url_fseek(pb, pos + 6, SEEK_SET);
135
    avio_wb32(pb, size);
136
    url_fseek(pb, curpos, SEEK_SET);
137
    return curpos - pos;
138
}
139

    
140
static int64_t updateSize(AVIOContext *pb, int64_t pos)
141
{
142
    int64_t curpos;
143

    
144
    curpos = url_ftell(pb);
145
    url_fseek(pb, pos, SEEK_SET);
146
    avio_wb16(pb, curpos - pos - 2);
147
    url_fseek(pb, curpos, SEEK_SET);
148
    return curpos - pos;
149
}
150

    
151
static void gxf_write_packet_header(AVIOContext *pb, GXFPktType type)
152
{
153
    avio_wb32(pb, 0);  /* packet leader for synchro */
154
    avio_w8(pb, 1);
155
    avio_w8(pb, type); /* map packet */
156
    avio_wb32(pb, 0);  /* size */
157
    avio_wb32(pb, 0);  /* reserved */
158
    avio_w8(pb, 0xE1); /* trailer 1 */
159
    avio_w8(pb, 0xE2); /* trailer 2 */
160
}
161

    
162
static int gxf_write_mpeg_auxiliary(AVIOContext *pb, AVStream *st)
163
{
164
    GXFStreamContext *sc = st->priv_data;
165
    char buffer[1024];
166
    int size, starting_line;
167

    
168
    if (sc->iframes) {
169
        sc->p_per_gop = sc->pframes / sc->iframes;
170
        if (sc->pframes % sc->iframes)
171
            sc->p_per_gop++;
172
        if (sc->pframes) {
173
            sc->b_per_i_or_p = sc->bframes / sc->pframes;
174
            if (sc->bframes % sc->pframes)
175
                sc->b_per_i_or_p++;
176
        }
177
        if (sc->p_per_gop > 9)
178
            sc->p_per_gop = 9; /* ensure value won't take more than one char */
179
        if (sc->b_per_i_or_p > 9)
180
            sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */
181
    }
182
    if (st->codec->height == 512 || st->codec->height == 608)
183
        starting_line = 7; // VBI
184
    else if (st->codec->height == 480)
185
        starting_line = 20;
186
    else
187
        starting_line = 23; // default PAL
188

    
189
    size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
190
                    "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
191
                    (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
192
                    st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
193
                    starting_line, (st->codec->height + 15) / 16);
194
    avio_w8(pb, TRACK_MPG_AUX);
195
    avio_w8(pb, size + 1);
196
    avio_write(pb, (uint8_t *)buffer, size + 1);
197
    return size + 3;
198
}
199

    
200
static int gxf_write_timecode_auxiliary(AVIOContext *pb, GXFStreamContext *sc)
201
{
202
    avio_w8(pb, 0); /* fields */
203
    avio_w8(pb, 0); /* seconds */
204
    avio_w8(pb, 0); /* minutes */
205
    avio_w8(pb, 0); /* flags + hours */
206
    /* reserved */
207
    avio_wb32(pb, 0);
208
    return 8;
209
}
210

    
211
static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
212
{
213
    AVIOContext *pb = s->pb;
214
    int64_t pos;
215
    int mpeg = sc->track_type == 4 || sc->track_type == 9;
216

    
217
    /* track description section */
218
    avio_w8(pb, sc->media_type + 0x80);
219
    avio_w8(pb, index + 0xC0);
220

    
221
    pos = url_ftell(pb);
222
    avio_wb16(pb, 0); /* size */
223

    
224
    /* media file name */
225
    avio_w8(pb, TRACK_NAME);
226
    avio_w8(pb, strlen(ES_NAME_PATTERN) + 3);
227
    avio_write(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN) - 1);
228
    avio_wb16(pb, sc->media_info);
229
    avio_w8(pb, 0);
230

    
231
    if (!mpeg) {
232
        /* auxiliary information */
233
        avio_w8(pb, TRACK_AUX);
234
        avio_w8(pb, 8);
235
        if (sc->track_type == 3)
236
            gxf_write_timecode_auxiliary(pb, sc);
237
        else
238
            avio_wl64(pb, 0);
239
    }
240

    
241
    /* file system version */
242
    avio_w8(pb, TRACK_VER);
243
    avio_w8(pb, 4);
244
    avio_wb32(pb, 0);
245

    
246
    if (mpeg)
247
        gxf_write_mpeg_auxiliary(pb, s->streams[index]);
248

    
249
    /* frame rate */
250
    avio_w8(pb, TRACK_FPS);
251
    avio_w8(pb, 4);
252
    avio_wb32(pb, sc->frame_rate_index);
253

    
254
    /* lines per frame */
255
    avio_w8(pb, TRACK_LINES);
256
    avio_w8(pb, 4);
257
    avio_wb32(pb, sc->lines_index);
258

    
259
    /* fields per frame */
260
    avio_w8(pb, TRACK_FPF);
261
    avio_w8(pb, 4);
262
    avio_wb32(pb, sc->fields);
263

    
264
    return updateSize(pb, pos);
265
}
266

    
267
static int gxf_write_material_data_section(AVFormatContext *s)
268
{
269
    GXFContext *gxf = s->priv_data;
270
    AVIOContext *pb = s->pb;
271
    int64_t pos;
272
    int len;
273
    const char *filename = strrchr(s->filename, '/');
274

    
275
    pos = url_ftell(pb);
276
    avio_wb16(pb, 0); /* size */
277

    
278
    /* name */
279
    if (filename)
280
        filename++;
281
    else
282
        filename = s->filename;
283
    len = strlen(filename);
284

    
285
    avio_w8(pb, MAT_NAME);
286
    avio_w8(pb, strlen(SERVER_PATH) + len + 1);
287
    avio_write(pb, SERVER_PATH, sizeof(SERVER_PATH) - 1);
288
    avio_write(pb, filename, len);
289
    avio_w8(pb, 0);
290

    
291
    /* first field */
292
    avio_w8(pb, MAT_FIRST_FIELD);
293
    avio_w8(pb, 4);
294
    avio_wb32(pb, 0);
295

    
296
    /* last field */
297
    avio_w8(pb, MAT_LAST_FIELD);
298
    avio_w8(pb, 4);
299
    avio_wb32(pb, gxf->nb_fields);
300

    
301
    /* reserved */
302
    avio_w8(pb, MAT_MARK_IN);
303
    avio_w8(pb, 4);
304
    avio_wb32(pb, 0);
305

    
306
    avio_w8(pb, MAT_MARK_OUT);
307
    avio_w8(pb, 4);
308
    avio_wb32(pb, gxf->nb_fields);
309

    
310
    /* estimated size */
311
    avio_w8(pb, MAT_SIZE);
312
    avio_w8(pb, 4);
313
    avio_wb32(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
    AVIOContext *pb = s->pb;
322
    int64_t pos;
323
    int i;
324

    
325
    pos = url_ftell(pb);
326
    avio_wb16(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, int rewrite)
336
{
337
    GXFContext *gxf = s->priv_data;
338
    AVIOContext *pb = s->pb;
339
    int64_t pos = url_ftell(pb);
340

    
341
    if (!rewrite) {
342
        if (!(gxf->map_offsets_nb % 30)) {
343
            gxf->map_offsets = av_realloc(gxf->map_offsets,
344
                                          (gxf->map_offsets_nb+30)*sizeof(*gxf->map_offsets));
345
            if (!gxf->map_offsets) {
346
                av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n");
347
                return -1;
348
            }
349
        }
350
        gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here
351
    }
352

    
353
    gxf_write_packet_header(pb, PKT_MAP);
354

    
355
    /* preamble */
356
    avio_w8(pb, 0xE0); /* version */
357
    avio_w8(pb, 0xFF); /* reserved */
358

    
359
    gxf_write_material_data_section(s);
360
    gxf_write_track_description_section(s);
361

    
362
    return updatePacketSize(pb, pos);
363
}
364

    
365
static int gxf_write_flt_packet(AVFormatContext *s)
366
{
367
    GXFContext *gxf = s->priv_data;
368
    AVIOContext *pb = s->pb;
369
    int64_t pos = url_ftell(pb);
370
    int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
371
    int flt_entries = gxf->nb_fields / fields_per_flt;
372
    int i = 0;
373

    
374
    gxf_write_packet_header(pb, PKT_FLT);
375

    
376
    avio_wl32(pb, fields_per_flt); /* number of fields */
377
    avio_wl32(pb, flt_entries); /* number of active flt entries */
378

    
379
    if (gxf->flt_entries) {
380
        for (i = 0; i < flt_entries; i++)
381
            avio_wl32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
382
    }
383

    
384
    for (; i < 1000; i++)
385
        avio_wl32(pb, 0);
386

    
387
    return updatePacketSize(pb, pos);
388
}
389

    
390
static int gxf_write_umf_material_description(AVFormatContext *s)
391
{
392
    GXFContext *gxf = s->priv_data;
393
    AVIOContext *pb = s->pb;
394
    int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
395

    
396
    // XXX drop frame
397
    uint32_t timecode =
398
        gxf->nb_fields / (timecode_base * 3600) % 24 << 24 | // hours
399
        gxf->nb_fields / (timecode_base * 60) % 60   << 16 | // minutes
400
        gxf->nb_fields /  timecode_base % 60         <<  8 | // seconds
401
        gxf->nb_fields %  timecode_base;                     // fields
402

    
403
    avio_wl32(pb, gxf->flags);
404
    avio_wl32(pb, gxf->nb_fields); /* length of the longest track */
405
    avio_wl32(pb, gxf->nb_fields); /* length of the shortest track */
406
    avio_wl32(pb, 0); /* mark in */
407
    avio_wl32(pb, gxf->nb_fields); /* mark out */
408
    avio_wl32(pb, 0); /* timecode mark in */
409
    avio_wl32(pb, timecode); /* timecode mark out */
410
    avio_wl64(pb, s->timestamp); /* modification time */
411
    avio_wl64(pb, s->timestamp); /* creation time */
412
    avio_wl16(pb, 0); /* reserved */
413
    avio_wl16(pb, 0); /* reserved */
414
    avio_wl16(pb, gxf->audio_tracks);
415
    avio_wl16(pb, 1); /* timecode track count */
416
    avio_wl16(pb, 0); /* reserved */
417
    avio_wl16(pb, gxf->mpeg_tracks);
418
    return 48;
419
}
420

    
421
static int gxf_write_umf_payload(AVFormatContext *s)
422
{
423
    GXFContext *gxf = s->priv_data;
424
    AVIOContext *pb = s->pb;
425

    
426
    avio_wl32(pb, gxf->umf_length); /* total length of the umf data */
427
    avio_wl32(pb, 3); /* version */
428
    avio_wl32(pb, s->nb_streams+1);
429
    avio_wl32(pb, gxf->umf_track_offset); /* umf track section offset */
430
    avio_wl32(pb, gxf->umf_track_size);
431
    avio_wl32(pb, s->nb_streams+1);
432
    avio_wl32(pb, gxf->umf_media_offset);
433
    avio_wl32(pb, gxf->umf_media_size);
434
    avio_wl32(pb, gxf->umf_length); /* user data offset */
435
    avio_wl32(pb, 0); /* user data size */
436
    avio_wl32(pb, 0); /* reserved */
437
    avio_wl32(pb, 0); /* reserved */
438
    return 48;
439
}
440

    
441
static int gxf_write_umf_track_description(AVFormatContext *s)
442
{
443
    AVIOContext *pb = s->pb;
444
    GXFContext *gxf = s->priv_data;
445
    int64_t pos = url_ftell(pb);
446
    int i;
447

    
448
    gxf->umf_track_offset = pos - gxf->umf_start_offset;
449
    for (i = 0; i < s->nb_streams; ++i) {
450
        GXFStreamContext *sc = s->streams[i]->priv_data;
451
        avio_wl16(pb, sc->media_info);
452
        avio_wl16(pb, 1);
453
    }
454

    
455
    avio_wl16(pb, gxf->timecode_track.media_info);
456
    avio_wl16(pb, 1);
457

    
458
    return url_ftell(pb) - pos;
459
}
460

    
461
static int gxf_write_umf_media_mpeg(AVIOContext *pb, AVStream *st)
462
{
463
    GXFStreamContext *sc = st->priv_data;
464

    
465
    if (st->codec->pix_fmt == PIX_FMT_YUV422P)
466
        avio_wl32(pb, 2);
467
    else
468
        avio_wl32(pb, 1); /* default to 420 */
469
    avio_wl32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
470
    avio_wl32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
471
    avio_wl32(pb, 1); /* I picture per GOP */
472
    avio_wl32(pb, sc->p_per_gop);
473
    avio_wl32(pb, sc->b_per_i_or_p);
474
    if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
475
        avio_wl32(pb, 2);
476
    else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO)
477
        avio_wl32(pb, 1);
478
    else
479
        avio_wl32(pb, 0);
480
    avio_wl32(pb, 0); /* reserved */
481
    return 32;
482
}
483

    
484
static int gxf_write_umf_media_timecode(AVIOContext *pb, GXFStreamContext *sc)
485
{
486
    avio_wl32(pb, 1); /* non drop frame */
487
    avio_wl32(pb, 0); /* reserved */
488
    avio_wl32(pb, 0); /* reserved */
489
    avio_wl32(pb, 0); /* reserved */
490
    avio_wl32(pb, 0); /* reserved */
491
    avio_wl32(pb, 0); /* reserved */
492
    avio_wl32(pb, 0); /* reserved */
493
    avio_wl32(pb, 0); /* reserved */
494
    return 32;
495
}
496

    
497
static int gxf_write_umf_media_dv(AVIOContext *pb, GXFStreamContext *sc)
498
{
499
    int i;
500

    
501
    for (i = 0; i < 8; i++) {
502
        avio_wb32(pb, 0);
503
    }
504
    return 32;
505
}
506

    
507
static int gxf_write_umf_media_audio(AVIOContext *pb, GXFStreamContext *sc)
508
{
509
    avio_wl64(pb, av_dbl2int(1)); /* sound level to begin to */
510
    avio_wl64(pb, av_dbl2int(1)); /* sound level to begin to */
511
    avio_wl32(pb, 0); /* number of fields over which to ramp up sound level */
512
    avio_wl32(pb, 0); /* number of fields over which to ramp down sound level */
513
    avio_wl32(pb, 0); /* reserved */
514
    avio_wl32(pb, 0); /* reserved */
515
    return 32;
516
}
517

    
518
#if 0
519
static int gxf_write_umf_media_mjpeg(AVIOContext *pb, GXFStreamContext *sc)
520
{
521
    avio_wb64(pb, 0); /* FIXME FLOAT max chroma quant level */
522
    avio_wb64(pb, 0); /* FIXME FLOAT max luma quant level */
523
    avio_wb64(pb, 0); /* FIXME FLOAT min chroma quant level */
524
    avio_wb64(pb, 0); /* FIXME FLOAT min luma quant level */
525
    return 32;
526
}
527
#endif
528

    
529
static int gxf_write_umf_media_description(AVFormatContext *s)
530
{
531
    GXFContext *gxf = s->priv_data;
532
    AVIOContext *pb = s->pb;
533
    int64_t pos;
534
    int i, j;
535

    
536
    pos = url_ftell(pb);
537
    gxf->umf_media_offset = pos - gxf->umf_start_offset;
538
    for (i = 0; i <= s->nb_streams; ++i) {
539
        GXFStreamContext *sc;
540
        int64_t startpos, curpos;
541

    
542
        if (i == s->nb_streams)
543
            sc = &gxf->timecode_track;
544
        else
545
            sc = s->streams[i]->priv_data;
546

    
547
        startpos = url_ftell(pb);
548
        avio_wl16(pb, 0); /* length */
549
        avio_wl16(pb, sc->media_info);
550
        avio_wl16(pb, 0); /* reserved */
551
        avio_wl16(pb, 0); /* reserved */
552
        avio_wl32(pb, gxf->nb_fields);
553
        avio_wl32(pb, 0); /* attributes rw, ro */
554
        avio_wl32(pb, 0); /* mark in */
555
        avio_wl32(pb, gxf->nb_fields); /* mark out */
556
        avio_write(pb, ES_NAME_PATTERN, strlen(ES_NAME_PATTERN));
557
        avio_wb16(pb, sc->media_info);
558
        for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
559
            avio_w8(pb, 0);
560
        avio_wl32(pb, sc->track_type);
561
        avio_wl32(pb, sc->sample_rate);
562
        avio_wl32(pb, sc->sample_size);
563
        avio_wl32(pb, 0); /* reserved */
564

    
565
        if (sc == &gxf->timecode_track)
566
            gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
567
        else {
568
            AVStream *st = s->streams[i];
569
            switch (st->codec->codec_id) {
570
            case CODEC_ID_MPEG1VIDEO:
571
            case CODEC_ID_MPEG2VIDEO:
572
                gxf_write_umf_media_mpeg(pb, st);
573
                break;
574
            case CODEC_ID_PCM_S16LE:
575
                gxf_write_umf_media_audio(pb, sc);
576
                break;
577
            case CODEC_ID_DVVIDEO:
578
                gxf_write_umf_media_dv(pb, sc);
579
                break;
580
            }
581
        }
582

    
583
        curpos = url_ftell(pb);
584
        url_fseek(pb, startpos, SEEK_SET);
585
        avio_wl16(pb, curpos - startpos);
586
        url_fseek(pb, curpos, SEEK_SET);
587
    }
588
    return url_ftell(pb) - pos;
589
}
590

    
591
static int gxf_write_umf_packet(AVFormatContext *s)
592
{
593
    GXFContext *gxf = s->priv_data;
594
    AVIOContext *pb = s->pb;
595
    int64_t pos = url_ftell(pb);
596

    
597
    gxf_write_packet_header(pb, PKT_UMF);
598

    
599
    /* preamble */
600
    avio_w8(pb, 3); /* first and last (only) packet */
601
    avio_wb32(pb, gxf->umf_length); /* data length */
602

    
603
    gxf->umf_start_offset = url_ftell(pb);
604
    gxf_write_umf_payload(s);
605
    gxf_write_umf_material_description(s);
606
    gxf->umf_track_size = gxf_write_umf_track_description(s);
607
    gxf->umf_media_size = gxf_write_umf_media_description(s);
608
    gxf->umf_length = url_ftell(pb) - gxf->umf_start_offset;
609
    return updatePacketSize(pb, pos);
610
}
611

    
612
static const int GXF_samples_per_frame[] = { 32768, 0 };
613

    
614
static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
615
{
616
    if (!vsc)
617
        return;
618

    
619
    sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
620
    sc->sample_rate = vsc->sample_rate;
621
    sc->media_info = ('T'<<8) | '0';
622
    sc->track_type = 3;
623
    sc->frame_rate_index = vsc->frame_rate_index;
624
    sc->lines_index = vsc->lines_index;
625
    sc->sample_size = 16;
626
    sc->fields = vsc->fields;
627
}
628

    
629
static int gxf_write_header(AVFormatContext *s)
630
{
631
    AVIOContext *pb = s->pb;
632
    GXFContext *gxf = s->priv_data;
633
    GXFStreamContext *vsc = NULL;
634
    uint8_t tracks[255] = {0};
635
    int i, media_info = 0;
636

    
637
    if (url_is_streamed(pb)) {
638
        av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome");
639
        return -1;
640
    }
641

    
642
    gxf->flags |= 0x00080000; /* material is simple clip */
643
    for (i = 0; i < s->nb_streams; ++i) {
644
        AVStream *st = s->streams[i];
645
        GXFStreamContext *sc = av_mallocz(sizeof(*sc));
646
        if (!sc)
647
            return AVERROR(ENOMEM);
648
        st->priv_data = sc;
649

    
650
        sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
651
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
652
            if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
653
                av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
654
                return -1;
655
            }
656
            if (st->codec->sample_rate != 48000) {
657
                av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
658
                return -1;
659
            }
660
            if (st->codec->channels != 1) {
661
                av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
662
                return -1;
663
            }
664
            sc->track_type = 2;
665
            sc->sample_rate = st->codec->sample_rate;
666
            av_set_pts_info(st, 64, 1, sc->sample_rate);
667
            sc->sample_size = 16;
668
            sc->frame_rate_index = -2;
669
            sc->lines_index = -2;
670
            sc->fields = -2;
671
            gxf->audio_tracks++;
672
            gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
673
            media_info = 'A';
674
        } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
675
            if (i != 0) {
676
                av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
677
                return -1;
678
            }
679
            /* FIXME check from time_base ? */
680
            if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
681
                sc->frame_rate_index = 5;
682
                sc->sample_rate = 60;
683
                gxf->flags |= 0x00000080;
684
                gxf->time_base = (AVRational){ 1001, 60000 };
685
            } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
686
                sc->frame_rate_index = 6;
687
                sc->media_type++;
688
                sc->sample_rate = 50;
689
                gxf->flags |= 0x00000040;
690
                gxf->time_base = (AVRational){ 1, 50 };
691
            } else {
692
                av_log(s, AV_LOG_ERROR, "unsupported video resolution, "
693
                       "gxf muxer only accepts PAL or NTSC resolutions currently\n");
694
                return -1;
695
            }
696
            av_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
697
            if (gxf_find_lines_index(st) < 0)
698
                sc->lines_index = -1;
699
            sc->sample_size = st->codec->bit_rate;
700
            sc->fields = 2; /* interlaced */
701

    
702
            vsc = sc;
703

    
704
            switch (st->codec->codec_id) {
705
            case CODEC_ID_MJPEG:
706
                sc->track_type = 1;
707
                gxf->flags |= 0x00004000;
708
                media_info = 'J';
709
                break;
710
            case CODEC_ID_MPEG1VIDEO:
711
                sc->track_type = 9;
712
                gxf->mpeg_tracks++;
713
                media_info = 'L';
714
                break;
715
            case CODEC_ID_MPEG2VIDEO:
716
                sc->first_gop_closed = -1;
717
                sc->track_type = 4;
718
                gxf->mpeg_tracks++;
719
                gxf->flags |= 0x00008000;
720
                media_info = 'M';
721
                break;
722
            case CODEC_ID_DVVIDEO:
723
                if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
724
                    sc->media_type += 2;
725
                    sc->track_type = 6;
726
                    gxf->flags |= 0x00002000;
727
                    media_info = 'E';
728
                } else {
729
                    sc->track_type = 5;
730
                    gxf->flags |= 0x00001000;
731
                    media_info = 'D';
732
                }
733
                break;
734
            default:
735
                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
736
                return -1;
737
            }
738
        }
739
        /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
740
        sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
741
        sc->order = s->nb_streams - st->index;
742
    }
743

    
744
    if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
745
        return -1;
746

    
747
    gxf_init_timecode_track(&gxf->timecode_track, vsc);
748
    gxf->flags |= 0x200000; // time code track is non-drop frame
749

    
750
    gxf_write_map_packet(s, 0);
751
    gxf_write_flt_packet(s);
752
    gxf_write_umf_packet(s);
753

    
754
    gxf->packet_count = 3;
755

    
756
    put_flush_packet(pb);
757
    return 0;
758
}
759

    
760
static int gxf_write_eos_packet(AVIOContext *pb)
761
{
762
    int64_t pos = url_ftell(pb);
763

    
764
    gxf_write_packet_header(pb, PKT_EOS);
765
    return updatePacketSize(pb, pos);
766
}
767

    
768
static int gxf_write_trailer(AVFormatContext *s)
769
{
770
    GXFContext *gxf = s->priv_data;
771
    AVIOContext *pb = s->pb;
772
    int64_t end;
773
    int i;
774

    
775
    ff_audio_interleave_close(s);
776

    
777
    gxf_write_eos_packet(pb);
778
    end = url_ftell(pb);
779
    url_fseek(pb, 0, SEEK_SET);
780
    /* overwrite map, flt and umf packets with new values */
781
    gxf_write_map_packet(s, 1);
782
    gxf_write_flt_packet(s);
783
    gxf_write_umf_packet(s);
784
    put_flush_packet(pb);
785
    /* update duration in all map packets */
786
    for (i = 1; i < gxf->map_offsets_nb; i++) {
787
        url_fseek(pb, gxf->map_offsets[i], SEEK_SET);
788
        gxf_write_map_packet(s, 1);
789
        put_flush_packet(pb);
790
    }
791

    
792
    url_fseek(pb, end, SEEK_SET);
793

    
794
    av_freep(&gxf->flt_entries);
795
    av_freep(&gxf->map_offsets);
796

    
797
    return 0;
798
}
799

    
800
static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
801
{
802
    uint32_t c=-1;
803
    int i;
804
    for(i=0; i<size-4 && c!=0x100; i++){
805
        c = (c<<8) + buf[i];
806
        if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
807
            sc->first_gop_closed= (buf[i+4]>>6)&1;
808
    }
809
    return (buf[i+1]>>3)&7;
810
}
811

    
812
static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
813
{
814
    GXFContext *gxf = s->priv_data;
815
    AVIOContext *pb = s->pb;
816
    AVStream *st = s->streams[pkt->stream_index];
817
    GXFStreamContext *sc = st->priv_data;
818
    unsigned field_nb;
819
    /* If the video is frame-encoded, the frame numbers shall be represented by
820
     * even field numbers.
821
     * see SMPTE360M-2004  6.4.2.1.3 Media field number */
822
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
823
        field_nb = gxf->nb_fields;
824
    } else {
825
        field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
826
                                  (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
827
    }
828

    
829
    avio_w8(pb, sc->media_type);
830
    avio_w8(pb, st->index);
831
    avio_wb32(pb, field_nb);
832
    if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
833
        avio_wb16(pb, 0);
834
        avio_wb16(pb, size / 2);
835
    } else if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
836
        int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
837
        if (frame_type == FF_I_TYPE) {
838
            avio_w8(pb, 0x0d);
839
            sc->iframes++;
840
        } else if (frame_type == FF_B_TYPE) {
841
            avio_w8(pb, 0x0f);
842
            sc->bframes++;
843
        } else {
844
            avio_w8(pb, 0x0e);
845
            sc->pframes++;
846
        }
847
        avio_wb24(pb, size);
848
    } else if (st->codec->codec_id == CODEC_ID_DVVIDEO) {
849
        avio_w8(pb, size / 4096);
850
        avio_wb24(pb, 0);
851
    } else
852
        avio_wb32(pb, size);
853
    avio_wb32(pb, field_nb);
854
    avio_w8(pb, 1); /* flags */
855
    avio_w8(pb, 0); /* reserved */
856
    return 16;
857
}
858

    
859
static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
860
{
861
    GXFContext *gxf = s->priv_data;
862
    AVIOContext *pb = s->pb;
863
    AVStream *st = s->streams[pkt->stream_index];
864
    int64_t pos = url_ftell(pb);
865
    int padding = 0;
866
    int packet_start_offset = url_ftell(pb) / 1024;
867

    
868
    gxf_write_packet_header(pb, PKT_MEDIA);
869
    if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
870
        padding = 4 - pkt->size % 4;
871
    else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
872
        padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
873
    gxf_write_media_preamble(s, pkt, pkt->size + padding);
874
    avio_write(pb, pkt->data, pkt->size);
875
    gxf_write_padding(pb, padding);
876

    
877
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
878
        if (!(gxf->flt_entries_nb % 500)) {
879
            gxf->flt_entries = av_realloc(gxf->flt_entries,
880
                                          (gxf->flt_entries_nb+500)*sizeof(*gxf->flt_entries));
881
            if (!gxf->flt_entries) {
882
                av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
883
                return -1;
884
            }
885
        }
886
        gxf->flt_entries[gxf->flt_entries_nb++] = packet_start_offset;
887
        gxf->nb_fields += 2; // count fields
888
    }
889

    
890
    updatePacketSize(pb, pos);
891

    
892
    gxf->packet_count++;
893
    if (gxf->packet_count == 100) {
894
        gxf_write_map_packet(s, 0);
895
        gxf->packet_count = 0;
896
    }
897

    
898
    put_flush_packet(pb);
899

    
900
    return 0;
901
}
902

    
903
static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
904
{
905
    GXFContext *gxf = s->priv_data;
906
    AVPacket *pkt[2] = { cur, next };
907
    int i, field_nb[2];
908
    GXFStreamContext *sc[2];
909

    
910
    for (i = 0; i < 2; i++) {
911
        AVStream *st = s->streams[pkt[i]->stream_index];
912
        sc[i] = st->priv_data;
913
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
914
            field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
915
                                         (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
916
            field_nb[i] &= ~1; // compare against even field number because audio must be before video
917
        } else
918
            field_nb[i] = pkt[i]->dts; // dts are field based
919
    }
920

    
921
    return field_nb[1] > field_nb[0] ||
922
        (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
923
}
924

    
925
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
926
{
927
    if (pkt && s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
928
        pkt->duration = 2; // enforce 2 fields
929
    return ff_audio_rechunk_interleave(s, out, pkt, flush,
930
                               av_interleave_packet_per_dts, gxf_compare_field_nb);
931
}
932

    
933
AVOutputFormat ff_gxf_muxer = {
934
    "gxf",
935
    NULL_IF_CONFIG_SMALL("GXF format"),
936
    NULL,
937
    "gxf",
938
    sizeof(GXFContext),
939
    CODEC_ID_PCM_S16LE,
940
    CODEC_ID_MPEG2VIDEO,
941
    gxf_write_header,
942
    gxf_write_packet,
943
    gxf_write_trailer,
944
    0,
945
    NULL,
946
    gxf_interleave_packet,
947
};