gxfenc.c 30.7 KB
Newer Older
Baptiste Coudurier's avatar
Baptiste Coudurier committed
1 2
/*
 * GXF muxer.
3
 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
Baptiste Coudurier's avatar
Baptiste Coudurier committed
4
 *
5 6
 * This file is part of FFmpeg.
 *
7 8 9 10
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
Baptiste Coudurier's avatar
Baptiste Coudurier committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Baptiste Coudurier's avatar
Baptiste Coudurier committed
16
 *
17 18
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Baptiste Coudurier's avatar
Baptiste Coudurier committed
20 21
 */

22
#include "libavutil/avassert.h"
23
#include "libavutil/intfloat.h"
24
#include "libavutil/opt.h"
25
#include "libavutil/mathematics.h"
26
#include "libavutil/timecode.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
27
#include "avformat.h"
28
#include "internal.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
29
#include "gxf.h"
30
#include "audiointerleave.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
31 32 33

#define GXF_AUDIO_PACKET_SIZE 65536

34 35 36 37 38 39 40 41 42 43 44 45
#define GXF_TIMECODE(c, d, h, m, s, f) \
    ((c) << 30 | (d) << 29 | (h) << 24 | (m) << 16 | (s) << 8 | (f))

typedef struct GXFTimecode{
    int hh;
    int mm;
    int ss;
    int ff;
    int color;
    int drop;
} GXFTimecode;

Baptiste Coudurier's avatar
Baptiste Coudurier committed
46
typedef struct GXFStreamContext {
47
    AudioInterleaveContext aic;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
48 49 50 51 52 53 54 55 56 57 58 59
    uint32_t track_type;
    uint32_t sample_size;
    uint32_t sample_rate;
    uint16_t media_type;
    uint16_t media_info;
    int frame_rate_index;
    int lines_index;
    int fields;
    int iframes;
    int pframes;
    int bframes;
    int p_per_gop;
60
    int b_per_i_or_p; ///< number of B frames per I frame or P frame
61
    int first_gop_closed;
62
    unsigned order;   ///< interleaving order
Baptiste Coudurier's avatar
Baptiste Coudurier committed
63 64 65
} GXFStreamContext;

typedef struct GXFContext {
66
    AVClass *av_class;
67
    uint32_t nb_fields;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
68 69 70 71 72 73 74 75 76
    uint16_t audio_tracks;
    uint16_t mpeg_tracks;
    int64_t creation_time;
    uint32_t umf_start_offset;
    uint32_t umf_track_offset;
    uint32_t umf_media_offset;
    uint32_t umf_length;
    uint16_t umf_track_size;
    uint16_t umf_media_size;
77
    AVRational time_base;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
78
    int flags;
79
    GXFStreamContext timecode_track;
80 81
    unsigned *flt_entries;    ///< offsets of packets /1024, starts after 2nd video field
    unsigned flt_entries_nb;
82 83 84
    uint64_t *map_offsets;    ///< offset of map packets
    unsigned map_offsets_nb;
    unsigned packet_count;
85
    GXFTimecode tc;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
86 87
} GXFContext;

88 89 90
static const struct {
    int height, index;
} gxf_lines_tab[] = {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
91 92 93 94 95 96 97 98
    { 480,  1 }, /* NTSC */
    { 512,  1 }, /* NTSC + VBI */
    { 576,  2 }, /* PAL */
    { 608,  2 }, /* PAL + VBI */
    { 1080, 4 },
    { 720,  6 },
};

99
static const AVCodecTag gxf_media_types[] = {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    { AV_CODEC_ID_MJPEG     ,   3 }, /* NTSC */
    { AV_CODEC_ID_MJPEG     ,   4 }, /* PAL */
    { AV_CODEC_ID_PCM_S24LE ,   9 },
    { AV_CODEC_ID_PCM_S16LE ,  10 },
    { AV_CODEC_ID_MPEG2VIDEO,  11 }, /* NTSC */
    { AV_CODEC_ID_MPEG2VIDEO,  12 }, /* PAL */
    { AV_CODEC_ID_DVVIDEO   ,  13 }, /* NTSC */
    { AV_CODEC_ID_DVVIDEO   ,  14 }, /* PAL */
    { AV_CODEC_ID_DVVIDEO   ,  15 }, /* 50M NTSC */
    { AV_CODEC_ID_DVVIDEO   ,  16 }, /* 50M PAL */
    { AV_CODEC_ID_AC3       ,  17 },
    //{ AV_CODEC_ID_NONE,  ,   18 }, /* Non compressed 24 bit audio */
    { AV_CODEC_ID_MPEG2VIDEO,  20 }, /* MPEG HD */
    { AV_CODEC_ID_MPEG1VIDEO,  22 }, /* NTSC */
    { AV_CODEC_ID_MPEG1VIDEO,  23 }, /* PAL */
    { AV_CODEC_ID_NONE,         0 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
116 117
};

118 119
#define SERVER_PATH "EXT:/PDR/default/"
#define ES_NAME_PATTERN "EXT:/PDR/default/ES."
Baptiste Coudurier's avatar
Baptiste Coudurier committed
120

121
static int gxf_find_lines_index(AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
122
{
123
    GXFStreamContext *sc = st->priv_data;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
124 125 126
    int i;

    for (i = 0; i < 6; ++i) {
127 128
        if (st->codec->height == gxf_lines_tab[i].height) {
            sc->lines_index = gxf_lines_tab[i].index;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
129 130 131 132 133 134
            return 0;
        }
    }
    return -1;
}

135
static void gxf_write_padding(AVIOContext *pb, int64_t to_pad)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
136
{
137
    for (; to_pad > 0; to_pad--) {
138
        avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
139 140 141
    }
}

142
static int64_t updatePacketSize(AVIOContext *pb, int64_t pos)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
143
{
144
    int64_t curpos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
145 146
    int size;

147
    size = avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
148 149
    if (size % 4) {
        gxf_write_padding(pb, 4 - size % 4);
150
        size = avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
151
    }
152
    curpos = avio_tell(pb);
153
    avio_seek(pb, pos + 6, SEEK_SET);
154
    avio_wb32(pb, size);
155
    avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
156 157 158
    return curpos - pos;
}

159
static int64_t updateSize(AVIOContext *pb, int64_t pos)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
160
{
161
    int64_t curpos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
162

163
    curpos = avio_tell(pb);
164
    avio_seek(pb, pos, SEEK_SET);
165
    avio_wb16(pb, curpos - pos - 2);
166
    avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
167 168 169
    return curpos - pos;
}

170
static void gxf_write_packet_header(AVIOContext *pb, GXFPktType type)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
171
{
172 173 174 175 176 177 178
    avio_wb32(pb, 0);  /* packet leader for synchro */
    avio_w8(pb, 1);
    avio_w8(pb, type); /* map packet */
    avio_wb32(pb, 0);  /* size */
    avio_wb32(pb, 0);  /* reserved */
    avio_w8(pb, 0xE1); /* trailer 1 */
    avio_w8(pb, 0xE2); /* trailer 2 */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
179 180
}

181
static int gxf_write_mpeg_auxiliary(AVIOContext *pb, AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
182
{
183
    GXFStreamContext *sc = st->priv_data;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
184
    char buffer[1024];
185
    int size, starting_line;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
186

187 188 189 190 191 192 193 194
    if (sc->iframes) {
        sc->p_per_gop = sc->pframes / sc->iframes;
        if (sc->pframes % sc->iframes)
            sc->p_per_gop++;
        if (sc->pframes) {
            sc->b_per_i_or_p = sc->bframes / sc->pframes;
            if (sc->bframes % sc->pframes)
                sc->b_per_i_or_p++;
195
        }
196 197 198 199
        if (sc->p_per_gop > 9)
            sc->p_per_gop = 9; /* ensure value won't take more than one char */
        if (sc->b_per_i_or_p > 9)
            sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
200
    }
201
    if (st->codec->height == 512 || st->codec->height == 608)
202
        starting_line = 7; // VBI
203
    else if (st->codec->height == 480)
204 205 206 207
        starting_line = 20;
    else
        starting_line = 23; // default PAL

208
    size = snprintf(buffer, sizeof(buffer), "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
209
                    "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
210
                    (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
211
                    st->codec->pix_fmt == AV_PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
212
                    starting_line, (st->codec->height + 15) / 16);
213
    av_assert0(size < sizeof(buffer));
214 215 216
    avio_w8(pb, TRACK_MPG_AUX);
    avio_w8(pb, size + 1);
    avio_write(pb, (uint8_t *)buffer, size + 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
217 218 219
    return size + 3;
}

220
static int gxf_write_timecode_auxiliary(AVIOContext *pb, GXFContext *gxf)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
221
{
222 223 224 225 226
    uint32_t timecode = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
                                     gxf->tc.hh, gxf->tc.mm,
                                     gxf->tc.ss, gxf->tc.ff);

    avio_wl32(pb, timecode);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
227
    /* reserved */
228
    avio_wl32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
229 230 231
    return 8;
}

232
static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
233
{
234
    GXFContext *gxf = s->priv_data;
235
    AVIOContext *pb = s->pb;
236
    int64_t pos;
237
    int mpeg = sc->track_type == 4 || sc->track_type == 9;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
238 239

    /* track description section */
240 241
    avio_w8(pb, sc->media_type + 0x80);
    avio_w8(pb, index + 0xC0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
242

243
    pos = avio_tell(pb);
244
    avio_wb16(pb, 0); /* size */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
245 246

    /* media file name */
247 248
    avio_w8(pb, TRACK_NAME);
    avio_w8(pb, strlen(ES_NAME_PATTERN) + 3);
249
    avio_write(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN) - 1);
250 251
    avio_wb16(pb, sc->media_info);
    avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
252

253
    if (!mpeg) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
254
        /* auxiliary information */
255 256
        avio_w8(pb, TRACK_AUX);
        avio_w8(pb, 8);
257
        if (sc->track_type == 3)
258
            gxf_write_timecode_auxiliary(pb, gxf);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
259
        else
260
            avio_wl64(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
261 262 263
    }

    /* file system version */
264 265 266
    avio_w8(pb, TRACK_VER);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
267

268 269
    if (mpeg)
        gxf_write_mpeg_auxiliary(pb, s->streams[index]);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
270 271

    /* frame rate */
272 273 274
    avio_w8(pb, TRACK_FPS);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->frame_rate_index);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
275 276

    /* lines per frame */
277 278 279
    avio_w8(pb, TRACK_LINES);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->lines_index);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
280 281

    /* fields per frame */
282 283 284
    avio_w8(pb, TRACK_FPF);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
285 286 287 288

    return updateSize(pb, pos);
}

289
static int gxf_write_material_data_section(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
290
{
291
    GXFContext *gxf = s->priv_data;
292
    AVIOContext *pb = s->pb;
293
    int64_t pos;
294
    int len;
295
    const char *filename = strrchr(s->filename, '/');
Baptiste Coudurier's avatar
Baptiste Coudurier committed
296

297
    pos = avio_tell(pb);
298
    avio_wb16(pb, 0); /* size */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
299 300 301 302 303

    /* name */
    if (filename)
        filename++;
    else
304
        filename = s->filename;
305 306
    len = strlen(filename);

307
    avio_w8(pb, MAT_NAME);
308 309 310
    avio_w8(pb, strlen(SERVER_PATH) + len + 1);
    avio_write(pb, SERVER_PATH, sizeof(SERVER_PATH) - 1);
    avio_write(pb, filename, len);
311
    avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
312 313

    /* first field */
314 315 316
    avio_w8(pb, MAT_FIRST_FIELD);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
317 318

    /* last field */
319 320 321
    avio_w8(pb, MAT_LAST_FIELD);
    avio_w8(pb, 4);
    avio_wb32(pb, gxf->nb_fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
322 323

    /* reserved */
324 325 326
    avio_w8(pb, MAT_MARK_IN);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
327

328 329 330
    avio_w8(pb, MAT_MARK_OUT);
    avio_w8(pb, 4);
    avio_wb32(pb, gxf->nb_fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
331 332

    /* estimated size */
333 334
    avio_w8(pb, MAT_SIZE);
    avio_w8(pb, 4);
335
    avio_wb32(pb, avio_size(pb) / 1024);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
336 337 338 339

    return updateSize(pb, pos);
}

340
static int gxf_write_track_description_section(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
341
{
342
    GXFContext *gxf = s->priv_data;
343
    AVIOContext *pb = s->pb;
344
    int64_t pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
345 346
    int i;

347
    pos = avio_tell(pb);
348
    avio_wb16(pb, 0); /* size */
349
    for (i = 0; i < s->nb_streams; ++i)
350 351 352 353
        gxf_write_track_description(s, s->streams[i]->priv_data, i);

    gxf_write_track_description(s, &gxf->timecode_track, s->nb_streams);

Baptiste Coudurier's avatar
Baptiste Coudurier committed
354 355 356
    return updateSize(pb, pos);
}

357
static int gxf_write_map_packet(AVFormatContext *s, int rewrite)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
358
{
359
    GXFContext *gxf = s->priv_data;
360
    AVIOContext *pb = s->pb;
361
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
362

363 364
    if (!rewrite) {
        if (!(gxf->map_offsets_nb % 30)) {
365 366 367
            gxf->map_offsets = av_realloc_f(gxf->map_offsets,
                                            sizeof(*gxf->map_offsets),
                                            gxf->map_offsets_nb+30);
368 369 370 371 372 373 374 375
            if (!gxf->map_offsets) {
                av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n");
                return -1;
            }
        }
        gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here
    }

Baptiste Coudurier's avatar
Baptiste Coudurier committed
376 377 378
    gxf_write_packet_header(pb, PKT_MAP);

    /* preamble */
379 380
    avio_w8(pb, 0xE0); /* version */
    avio_w8(pb, 0xFF); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
381

382 383
    gxf_write_material_data_section(s);
    gxf_write_track_description_section(s);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
384 385 386 387

    return updatePacketSize(pb, pos);
}

388
static int gxf_write_flt_packet(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
389
{
390
    GXFContext *gxf = s->priv_data;
391
    AVIOContext *pb = s->pb;
392
    int64_t pos = avio_tell(pb);
393
    int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
394
    int flt_entries = gxf->nb_fields / fields_per_flt;
395
    int i = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
396 397 398

    gxf_write_packet_header(pb, PKT_FLT);

399 400
    avio_wl32(pb, fields_per_flt); /* number of fields */
    avio_wl32(pb, flt_entries); /* number of active flt entries */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
401

402 403
    if (gxf->flt_entries) {
        for (i = 0; i < flt_entries; i++)
404
            avio_wl32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
405
    }
406 407

    for (; i < 1000; i++)
408
        avio_wl32(pb, 0);
409

Baptiste Coudurier's avatar
Baptiste Coudurier committed
410 411 412
    return updatePacketSize(pb, pos);
}

413
static int gxf_write_umf_material_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
414
{
415
    GXFContext *gxf = s->priv_data;
416
    AVIOContext *pb = s->pb;
417
    int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
418 419
    int64_t timestamp = 0;
    AVDictionaryEntry *t;
420 421 422
    uint64_t nb_fields;
    uint32_t timecode_in; // timecode at mark in
    uint32_t timecode_out; // timecode at mark out
423

424 425
    if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
        timestamp = ff_iso8601_to_unix_time(t->value);
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    timecode_in = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
                               gxf->tc.hh, gxf->tc.mm,
                               gxf->tc.ss, gxf->tc.ff);

    nb_fields = gxf->nb_fields +
                gxf->tc.hh * (timecode_base * 3600) +
                gxf->tc.mm * (timecode_base * 60)   +
                gxf->tc.ss * timecode_base          +
                gxf->tc.ff;

    timecode_out = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
                                nb_fields / (timecode_base * 3600) % 24,
                                nb_fields / (timecode_base * 60)   % 60,
                                nb_fields /  timecode_base % 60,
                                nb_fields %  timecode_base);
442

443 444 445 446 447
    avio_wl32(pb, gxf->flags);
    avio_wl32(pb, gxf->nb_fields); /* length of the longest track */
    avio_wl32(pb, gxf->nb_fields); /* length of the shortest track */
    avio_wl32(pb, 0); /* mark in */
    avio_wl32(pb, gxf->nb_fields); /* mark out */
448 449
    avio_wl32(pb, timecode_in); /* timecode mark in */
    avio_wl32(pb, timecode_out); /* timecode mark out */
450 451
    avio_wl64(pb, timestamp); /* modification time */
    avio_wl64(pb, timestamp); /* creation time */
452 453 454 455 456 457
    avio_wl16(pb, 0); /* reserved */
    avio_wl16(pb, 0); /* reserved */
    avio_wl16(pb, gxf->audio_tracks);
    avio_wl16(pb, 1); /* timecode track count */
    avio_wl16(pb, 0); /* reserved */
    avio_wl16(pb, gxf->mpeg_tracks);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
458 459 460
    return 48;
}

461
static int gxf_write_umf_payload(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
462
{
463
    GXFContext *gxf = s->priv_data;
464
    AVIOContext *pb = s->pb;
465

466 467 468 469 470 471 472 473 474 475 476 477
    avio_wl32(pb, gxf->umf_length); /* total length of the umf data */
    avio_wl32(pb, 3); /* version */
    avio_wl32(pb, s->nb_streams+1);
    avio_wl32(pb, gxf->umf_track_offset); /* umf track section offset */
    avio_wl32(pb, gxf->umf_track_size);
    avio_wl32(pb, s->nb_streams+1);
    avio_wl32(pb, gxf->umf_media_offset);
    avio_wl32(pb, gxf->umf_media_size);
    avio_wl32(pb, gxf->umf_length); /* user data offset */
    avio_wl32(pb, 0); /* user data size */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
478 479 480
    return 48;
}

481
static int gxf_write_umf_track_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
482
{
483
    AVIOContext *pb = s->pb;
484
    GXFContext *gxf = s->priv_data;
485
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
486 487
    int i;

488 489
    gxf->umf_track_offset = pos - gxf->umf_start_offset;
    for (i = 0; i < s->nb_streams; ++i) {
490
        GXFStreamContext *sc = s->streams[i]->priv_data;
491 492
        avio_wl16(pb, sc->media_info);
        avio_wl16(pb, 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
493
    }
494

495 496
    avio_wl16(pb, gxf->timecode_track.media_info);
    avio_wl16(pb, 1);
497

498
    return avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
499 500
}

501
static int gxf_write_umf_media_mpeg(AVIOContext *pb, AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
502
{
503 504
    GXFStreamContext *sc = st->priv_data;

505
    if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P)
506
        avio_wl32(pb, 2);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
507
    else
508 509 510 511 512 513
        avio_wl32(pb, 1); /* default to 420 */
    avio_wl32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
    avio_wl32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
    avio_wl32(pb, 1); /* I picture per GOP */
    avio_wl32(pb, sc->p_per_gop);
    avio_wl32(pb, sc->b_per_i_or_p);
514
    if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
515
        avio_wl32(pb, 2);
516
    else if (st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO)
517
        avio_wl32(pb, 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
518
    else
519 520
        avio_wl32(pb, 0);
    avio_wl32(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
521 522 523
    return 32;
}

524
static int gxf_write_umf_media_timecode(AVIOContext *pb, int drop)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
525
{
526
    avio_wl32(pb, drop); /* drop frame */
527 528 529 530 531 532 533
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
534 535 536
    return 32;
}

537
static int gxf_write_umf_media_dv(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
538 539 540 541
{
    int i;

    for (i = 0; i < 8; i++) {
542
        avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
543 544 545 546
    }
    return 32;
}

547
static int gxf_write_umf_media_audio(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
548
{
549 550
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
551 552 553 554
    avio_wl32(pb, 0); /* number of fields over which to ramp up sound level */
    avio_wl32(pb, 0); /* number of fields over which to ramp down sound level */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
555 556 557
    return 32;
}

558
static int gxf_write_umf_media_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
559
{
560
    GXFContext *gxf = s->priv_data;
561
    AVIOContext *pb = s->pb;
562
    int64_t pos;
563
    int i, j;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
564

565
    pos = avio_tell(pb);
566
    gxf->umf_media_offset = pos - gxf->umf_start_offset;
567 568
    for (i = 0; i <= s->nb_streams; ++i) {
        GXFStreamContext *sc;
569
        int64_t startpos, curpos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
570

571 572 573 574 575
        if (i == s->nb_streams)
            sc = &gxf->timecode_track;
        else
            sc = s->streams[i]->priv_data;

576
        startpos = avio_tell(pb);
577 578 579 580 581 582 583 584 585 586
        avio_wl16(pb, 0); /* length */
        avio_wl16(pb, sc->media_info);
        avio_wl16(pb, 0); /* reserved */
        avio_wl16(pb, 0); /* reserved */
        avio_wl32(pb, gxf->nb_fields);
        avio_wl32(pb, 0); /* attributes rw, ro */
        avio_wl32(pb, 0); /* mark in */
        avio_wl32(pb, gxf->nb_fields); /* mark out */
        avio_write(pb, ES_NAME_PATTERN, strlen(ES_NAME_PATTERN));
        avio_wb16(pb, sc->media_info);
587
        for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
588 589 590 591 592
            avio_w8(pb, 0);
        avio_wl32(pb, sc->track_type);
        avio_wl32(pb, sc->sample_rate);
        avio_wl32(pb, sc->sample_size);
        avio_wl32(pb, 0); /* reserved */
593 594

        if (sc == &gxf->timecode_track)
595
            gxf_write_umf_media_timecode(pb, gxf->tc.drop);
596 597
        else {
            AVStream *st = s->streams[i];
Baptiste Coudurier's avatar
Baptiste Coudurier committed
598
            switch (st->codec->codec_id) {
599 600
            case AV_CODEC_ID_MPEG1VIDEO:
            case AV_CODEC_ID_MPEG2VIDEO:
Baptiste Coudurier's avatar
Baptiste Coudurier committed
601 602
                gxf_write_umf_media_mpeg(pb, st);
                break;
603
            case AV_CODEC_ID_PCM_S16LE:
Baptiste Coudurier's avatar
Baptiste Coudurier committed
604 605
                gxf_write_umf_media_audio(pb, sc);
                break;
606
            case AV_CODEC_ID_DVVIDEO:
Baptiste Coudurier's avatar
Baptiste Coudurier committed
607 608 609
                gxf_write_umf_media_dv(pb, sc);
                break;
            }
610 611
        }

612
        curpos = avio_tell(pb);
613
        avio_seek(pb, startpos, SEEK_SET);
614
        avio_wl16(pb, curpos - startpos);
615
        avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
616
    }
617
    return avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
618 619
}

620
static int gxf_write_umf_packet(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
621
{
622
    GXFContext *gxf = s->priv_data;
623
    AVIOContext *pb = s->pb;
624
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
625 626 627 628

    gxf_write_packet_header(pb, PKT_UMF);

    /* preamble */
629 630
    avio_w8(pb, 3); /* first and last (only) packet */
    avio_wb32(pb, gxf->umf_length); /* data length */
631

632
    gxf->umf_start_offset = avio_tell(pb);
633 634 635 636
    gxf_write_umf_payload(s);
    gxf_write_umf_material_description(s);
    gxf->umf_track_size = gxf_write_umf_track_description(s);
    gxf->umf_media_size = gxf_write_umf_media_description(s);
637
    gxf->umf_length = avio_tell(pb) - gxf->umf_start_offset;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
638 639 640
    return updatePacketSize(pb, pos);
}

641 642
static const int GXF_samples_per_frame[] = { 32768, 0 };

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
{
    if (!vsc)
        return;

    sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
    sc->sample_rate = vsc->sample_rate;
    sc->media_info = ('T'<<8) | '0';
    sc->track_type = 3;
    sc->frame_rate_index = vsc->frame_rate_index;
    sc->lines_index = vsc->lines_index;
    sc->sample_size = 16;
    sc->fields = vsc->fields;
}

658
static int gxf_init_timecode(AVFormatContext *s, GXFTimecode *tc, const char *tcstr, int fields)
659 660 661
{
    char c;

662
    if (sscanf(tcstr, "%d:%d:%d%c%d", &tc->hh, &tc->mm, &tc->ss, &c, &tc->ff) != 5) {
663 664 665 666 667 668 669 670 671 672 673 674 675 676
        av_log(s, AV_LOG_ERROR, "unable to parse timecode, "
                                "syntax: hh:mm:ss[:;.]ff\n");
        return -1;
    }

    tc->color = 0;
    tc->drop = c != ':';

    if (fields == 2)
        tc->ff = tc->ff * 2;

    return 0;
}

Baptiste Coudurier's avatar
Baptiste Coudurier committed
677 678
static int gxf_write_header(AVFormatContext *s)
{
679
    AVIOContext *pb = s->pb;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
680
    GXFContext *gxf = s->priv_data;
681
    GXFStreamContext *vsc = NULL;
682 683
    uint8_t tracks[255] = {0};
    int i, media_info = 0;
684
    AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
685

686
    if (!pb->seekable) {
687
        av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome\n");
688 689 690
        return -1;
    }

Baptiste Coudurier's avatar
Baptiste Coudurier committed
691 692 693
    gxf->flags |= 0x00080000; /* material is simple clip */
    for (i = 0; i < s->nb_streams; ++i) {
        AVStream *st = s->streams[i];
694 695 696 697
        GXFStreamContext *sc = av_mallocz(sizeof(*sc));
        if (!sc)
            return AVERROR(ENOMEM);
        st->priv_data = sc;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
698

699
        sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
700
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
701
            if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
702 703 704 705 706 707 708 709 710 711 712 713 714
                av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
                return -1;
            }
            if (st->codec->sample_rate != 48000) {
                av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
                return -1;
            }
            if (st->codec->channels != 1) {
                av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
                return -1;
            }
            sc->track_type = 2;
            sc->sample_rate = st->codec->sample_rate;
715
            avpriv_set_pts_info(st, 64, 1, sc->sample_rate);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
716 717 718 719 720 721
            sc->sample_size = 16;
            sc->frame_rate_index = -2;
            sc->lines_index = -2;
            sc->fields = -2;
            gxf->audio_tracks++;
            gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
722
            media_info = 'A';
723
        } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
724 725 726 727
            if (i != 0) {
                av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
                return -1;
            }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
728
            /* FIXME check from time_base ? */
729
            if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
730 731 732
                sc->frame_rate_index = 5;
                sc->sample_rate = 60;
                gxf->flags |= 0x00000080;
733
                gxf->time_base = (AVRational){ 1001, 60000 };
734
            } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
735 736 737 738
                sc->frame_rate_index = 6;
                sc->media_type++;
                sc->sample_rate = 50;
                gxf->flags |= 0x00000040;
739
                gxf->time_base = (AVRational){ 1, 50 };
740 741 742 743
            } else {
                av_log(s, AV_LOG_ERROR, "unsupported video resolution, "
                       "gxf muxer only accepts PAL or NTSC resolutions currently\n");
                return -1;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
744
            }
745 746
            if (!tcr)
                tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
747
            avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
748
            if (gxf_find_lines_index(st) < 0)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
749 750 751
                sc->lines_index = -1;
            sc->sample_size = st->codec->bit_rate;
            sc->fields = 2; /* interlaced */
752 753 754

            vsc = sc;

755
            switch (st->codec->codec_id) {
756
            case AV_CODEC_ID_MJPEG:
757 758 759 760
                sc->track_type = 1;
                gxf->flags |= 0x00004000;
                media_info = 'J';
                break;
761
            case AV_CODEC_ID_MPEG1VIDEO:
762 763 764 765
                sc->track_type = 9;
                gxf->mpeg_tracks++;
                media_info = 'L';
                break;
766
            case AV_CODEC_ID_MPEG2VIDEO:
767
                sc->first_gop_closed = -1;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
768 769 770
                sc->track_type = 4;
                gxf->mpeg_tracks++;
                gxf->flags |= 0x00008000;
771
                media_info = 'M';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
772
                break;
773
            case AV_CODEC_ID_DVVIDEO:
774
                if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
775 776 777
                    sc->media_type += 2;
                    sc->track_type = 6;
                    gxf->flags |= 0x00002000;
778
                    media_info = 'E';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
779 780 781
                } else {
                    sc->track_type = 5;
                    gxf->flags |= 0x00001000;
782
                    media_info = 'D';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
783 784 785
                }
                break;
            default:
786
                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
787 788 789
                return -1;
            }
        }
790 791
        /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
        sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
792
        sc->order = s->nb_streams - st->index;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
793
    }
794 795 796 797

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

798
    if (tcr && vsc)
799
        gxf_init_timecode(s, &gxf->tc, tcr->value, vsc->fields);
800

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

804
    gxf_write_map_packet(s, 0);
805
    gxf_write_flt_packet(s);
806
    gxf_write_umf_packet(s);
807 808 809

    gxf->packet_count = 3;

810
    avio_flush(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
811 812 813
    return 0;
}

814
static int gxf_write_eos_packet(AVIOContext *pb)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
815
{
816
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
817 818 819 820 821 822 823

    gxf_write_packet_header(pb, PKT_EOS);
    return updatePacketSize(pb, pos);
}

static int gxf_write_trailer(AVFormatContext *s)
{
824
    GXFContext *gxf = s->priv_data;
825
    AVIOContext *pb = s->pb;
826
    int64_t end;
827
    int i;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
828

829
    ff_audio_interleave_close(s);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
830

831
    gxf_write_eos_packet(pb);
832
    end = avio_tell(pb);
833
    avio_seek(pb, 0, SEEK_SET);
834
    /* overwrite map, flt and umf packets with new values */
835
    gxf_write_map_packet(s, 1);
836
    gxf_write_flt_packet(s);
837
    gxf_write_umf_packet(s);
838
    avio_flush(pb);
839 840
    /* update duration in all map packets */
    for (i = 1; i < gxf->map_offsets_nb; i++) {
841
        avio_seek(pb, gxf->map_offsets[i], SEEK_SET);
842
        gxf_write_map_packet(s, 1);
843
        avio_flush(pb);
844 845
    }

846
    avio_seek(pb, end, SEEK_SET);
847 848

    av_freep(&gxf->flt_entries);
849
    av_freep(&gxf->map_offsets);
850

Baptiste Coudurier's avatar
Baptiste Coudurier committed
851 852 853
    return 0;
}

854 855 856 857 858 859
static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
{
    uint32_t c=-1;
    int i;
    for(i=0; i<size-4 && c!=0x100; i++){
        c = (c<<8) + buf[i];
860
        if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
861
            sc->first_gop_closed= (buf[i+4]>>6)&1;
862 863 864 865
    }
    return (buf[i+1]>>3)&7;
}

866
static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
867
{
868
    GXFContext *gxf = s->priv_data;
869
    AVIOContext *pb = s->pb;
870 871
    AVStream *st = s->streams[pkt->stream_index];
    GXFStreamContext *sc = st->priv_data;
872 873 874 875
    unsigned field_nb;
    /* If the video is frame-encoded, the frame numbers shall be represented by
     * even field numbers.
     * see SMPTE360M-2004  6.4.2.1.3 Media field number */
876
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
877
        field_nb = gxf->nb_fields;
878
    } else {
879 880
        field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
                                  (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
881
    }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
882

883 884 885
    avio_w8(pb, sc->media_type);
    avio_w8(pb, st->index);
    avio_wb32(pb, field_nb);
886
    if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
887 888
        avio_wb16(pb, 0);
        avio_wb16(pb, size / 2);
889
    } else if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
890
        int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
891
        if (frame_type == AV_PICTURE_TYPE_I) {
892
            avio_w8(pb, 0x0d);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
893
            sc->iframes++;
894
        } else if (frame_type == AV_PICTURE_TYPE_B) {
895
            avio_w8(pb, 0x0f);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
896 897
            sc->bframes++;
        } else {
898
            avio_w8(pb, 0x0e);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
899 900
            sc->pframes++;
        }
901
        avio_wb24(pb, size);
902
    } else if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) {
903 904
        avio_w8(pb, size / 4096);
        avio_wb24(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
905
    } else
906 907 908 909
        avio_wb32(pb, size);
    avio_wb32(pb, field_nb);
    avio_w8(pb, 1); /* flags */
    avio_w8(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
910 911 912
    return 16;
}

913
static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
914
{
915
    GXFContext *gxf = s->priv_data;
916
    AVIOContext *pb = s->pb;
917
    AVStream *st = s->streams[pkt->stream_index];
918
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
919
    int padding = 0;
920
    int packet_start_offset = avio_tell(pb) / 1024;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
921 922

    gxf_write_packet_header(pb, PKT_MEDIA);
923
    if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
924
        padding = 4 - pkt->size % 4;
925
    else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
926
        padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
927
    gxf_write_media_preamble(s, pkt, pkt->size + padding);
928
    avio_write(pb, pkt->data, pkt->size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
929
    gxf_write_padding(pb, padding);
930

931
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
932
        if (!(gxf->flt_entries_nb % 500)) {
933 934 935
            gxf->flt_entries = av_realloc_f(gxf->flt_entries,
                                            sizeof(*gxf->flt_entries),
                                            gxf->flt_entries_nb+500);
936 937 938 939 940
            if (!gxf->flt_entries) {
                av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
                return -1;
            }
        }
941
        gxf->flt_entries[gxf->flt_entries_nb++] = packet_start_offset;
942
        gxf->nb_fields += 2; // count fields
943
    }
944

945 946 947 948 949 950 951 952
    updatePacketSize(pb, pos);

    gxf->packet_count++;
    if (gxf->packet_count == 100) {
        gxf_write_map_packet(s, 0);
        gxf->packet_count = 0;
    }

953
    avio_flush(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
954

955
    return 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
956 957
}

958 959 960 961 962 963 964 965 966 967
static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
{
    GXFContext *gxf = s->priv_data;
    AVPacket *pkt[2] = { cur, next };
    int i, field_nb[2];
    GXFStreamContext *sc[2];

    for (i = 0; i < 2; i++) {
        AVStream *st = s->streams[pkt[i]->stream_index];
        sc[i] = st->priv_data;
968
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
969 970 971 972 973 974 975 976 977 978 979
            field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
                                         (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
            field_nb[i] &= ~1; // compare against even field number because audio must be before video
        } else
            field_nb[i] = pkt[i]->dts; // dts are field based
    }

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

Baptiste Coudurier's avatar
Baptiste Coudurier committed
980 981
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
{
982
    if (pkt && s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
983
        pkt->duration = 2; // enforce 2 fields
984
    return ff_audio_rechunk_interleave(s, out, pkt, flush,
985
                               ff_interleave_packet_per_dts, gxf_compare_field_nb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
986 987
}

988
AVOutputFormat ff_gxf_muxer = {
989
    .name              = "gxf",
990
    .long_name         = NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"),
991 992
    .extensions        = "gxf",
    .priv_data_size    = sizeof(GXFContext),
993 994
    .audio_codec       = AV_CODEC_ID_PCM_S16LE,
    .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
995 996 997 998
    .write_header      = gxf_write_header,
    .write_packet      = gxf_write_packet,
    .write_trailer     = gxf_write_trailer,
    .interleave_packet = gxf_interleave_packet,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
999
};