gxfenc.c 30.5 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/intfloat.h"
23
#include "libavutil/opt.h"
24
#include "libavutil/mathematics.h"
25
#include "libavutil/timecode.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
26
#include "avformat.h"
27
#include "internal.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
28 29
#include "gxf.h"
#include "riff.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[] = {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    { CODEC_ID_MJPEG     ,   3 }, /* NTSC */
    { CODEC_ID_MJPEG     ,   4 }, /* PAL */
    { CODEC_ID_PCM_S24LE ,   9 },
    { CODEC_ID_PCM_S16LE ,  10 },
    { CODEC_ID_MPEG2VIDEO,  11 }, /* NTSC */
    { CODEC_ID_MPEG2VIDEO,  12 }, /* PAL */
    { CODEC_ID_DVVIDEO   ,  13 }, /* NTSC */
    { CODEC_ID_DVVIDEO   ,  14 }, /* PAL */
    { CODEC_ID_DVVIDEO   ,  15 }, /* 50M NTSC */
    { CODEC_ID_DVVIDEO   ,  16 }, /* 50M PAL */
    { CODEC_ID_AC3       ,  17 },
    //{ CODEC_ID_NONE,  ,   18 }, /* Non compressed 24 bit audio */
    { CODEC_ID_MPEG2VIDEO,  20 }, /* MPEG HD */
    { CODEC_ID_MPEG1VIDEO,  22 }, /* NTSC */
    { CODEC_ID_MPEG1VIDEO,  23 }, /* PAL */
115
    { 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

Baptiste Coudurier's avatar
Baptiste Coudurier committed
208
    size = snprintf(buffer, 1024, "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 211
                    (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
                    st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
212
                    starting_line, (st->codec->height + 15) / 16);
213 214 215
    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
216 217 218
    return size + 3;
}

219
static int gxf_write_timecode_auxiliary(AVIOContext *pb, GXFContext *gxf)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
220
{
221 222 223 224 225
    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
226
    /* reserved */
227
    avio_wl32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
228 229 230
    return 8;
}

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

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

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

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

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

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

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

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

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

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

    return updateSize(pb, pos);
}

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

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

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

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

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

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

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

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

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

    return updateSize(pb, pos);
}

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

346
    pos = avio_tell(pb);
347
    avio_wb16(pb, 0); /* size */
348
    for (i = 0; i < s->nb_streams; ++i)
349 350 351 352
        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
353 354 355
    return updateSize(pb, pos);
}

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

362 363
    if (!rewrite) {
        if (!(gxf->map_offsets_nb % 30)) {
364 365 366
            gxf->map_offsets = av_realloc_f(gxf->map_offsets,
                                            sizeof(*gxf->map_offsets),
                                            gxf->map_offsets_nb+30);
367 368 369 370 371 372 373 374
            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
375 376 377
    gxf_write_packet_header(pb, PKT_MAP);

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

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

    return updatePacketSize(pb, pos);
}

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

    gxf_write_packet_header(pb, PKT_FLT);

398 399
    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
400

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

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

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

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

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

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    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);
441

442 443 444 445 446
    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 */
447 448
    avio_wl32(pb, timecode_in); /* timecode mark in */
    avio_wl32(pb, timecode_out); /* timecode mark out */
449 450
    avio_wl64(pb, timestamp); /* modification time */
    avio_wl64(pb, timestamp); /* creation time */
451 452 453 454 455 456
    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
457 458 459
    return 48;
}

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

465 466 467 468 469 470 471 472 473 474 475 476
    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
477 478 479
    return 48;
}

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

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

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

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

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

    if (st->codec->pix_fmt == PIX_FMT_YUV422P)
505
        avio_wl32(pb, 2);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
506
    else
507 508 509 510 511 512
        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);
513
    if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
514
        avio_wl32(pb, 2);
515
    else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO)
516
        avio_wl32(pb, 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
517
    else
518 519
        avio_wl32(pb, 0);
    avio_wl32(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
520 521 522
    return 32;
}

523
static int gxf_write_umf_media_timecode(AVIOContext *pb, int drop)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
524
{
525
    avio_wl32(pb, drop); /* drop frame */
526 527 528 529 530 531 532
    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
533 534 535
    return 32;
}

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

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

546
static int gxf_write_umf_media_audio(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
547
{
548 549
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
550 551 552 553
    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
554 555 556
    return 32;
}

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

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

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

575
        startpos = avio_tell(pb);
576 577 578 579 580 581 582 583 584 585
        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);
586
        for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
587 588 589 590 591
            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 */
592 593

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

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

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

    gxf_write_packet_header(pb, PKT_UMF);

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

631
    gxf->umf_start_offset = avio_tell(pb);
632 633 634 635
    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);
636
    gxf->umf_length = avio_tell(pb) - gxf->umf_start_offset;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
637 638 639
    return updatePacketSize(pb, pos);
}

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

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
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;
}

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

661
    if (sscanf(tcstr, "%d:%d:%d%c%d", &tc->hh, &tc->mm, &tc->ss, &c, &tc->ff) != 5) {
662 663 664 665 666 667 668 669 670 671 672 673 674 675
        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
676 677
static int gxf_write_header(AVFormatContext *s)
{
678
    AVIOContext *pb = s->pb;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
679
    GXFContext *gxf = s->priv_data;
680
    GXFStreamContext *vsc = NULL;
681 682
    uint8_t tracks[255] = {0};
    int i, media_info = 0;
683
    AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
684

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

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

698
        sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
699
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713
            if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
                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;
714
            avpriv_set_pts_info(st, 64, 1, sc->sample_rate);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
715 716 717 718 719 720
            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 */
721
            media_info = 'A';
722
        } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
723 724 725 726
            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
727
            /* FIXME check from time_base ? */
728
            if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
729 730 731
                sc->frame_rate_index = 5;
                sc->sample_rate = 60;
                gxf->flags |= 0x00000080;
732
                gxf->time_base = (AVRational){ 1001, 60000 };
733
            } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
734 735 736 737
                sc->frame_rate_index = 6;
                sc->media_type++;
                sc->sample_rate = 50;
                gxf->flags |= 0x00000040;
738
                gxf->time_base = (AVRational){ 1, 50 };
739 740 741 742
            } 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
743
            }
744 745
            if (!tcr)
                tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
746
            avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
747
            if (gxf_find_lines_index(st) < 0)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
748 749 750
                sc->lines_index = -1;
            sc->sample_size = st->codec->bit_rate;
            sc->fields = 2; /* interlaced */
751 752 753

            vsc = sc;

754
            switch (st->codec->codec_id) {
755 756 757 758 759 760 761 762 763 764
            case CODEC_ID_MJPEG:
                sc->track_type = 1;
                gxf->flags |= 0x00004000;
                media_info = 'J';
                break;
            case CODEC_ID_MPEG1VIDEO:
                sc->track_type = 9;
                gxf->mpeg_tracks++;
                media_info = 'L';
                break;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
765
            case CODEC_ID_MPEG2VIDEO:
766
                sc->first_gop_closed = -1;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
767 768 769
                sc->track_type = 4;
                gxf->mpeg_tracks++;
                gxf->flags |= 0x00008000;
770
                media_info = 'M';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
771 772
                break;
            case CODEC_ID_DVVIDEO:
773
                if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
774 775 776
                    sc->media_type += 2;
                    sc->track_type = 6;
                    gxf->flags |= 0x00002000;
777
                    media_info = 'E';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
778 779 780
                } else {
                    sc->track_type = 5;
                    gxf->flags |= 0x00001000;
781
                    media_info = 'D';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
782 783 784
                }
                break;
            default:
785
                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
786 787 788
                return -1;
            }
        }
789 790
        /* 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]++);
791
        sc->order = s->nb_streams - st->index;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
792
    }
793 794 795 796

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

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

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

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

    gxf->packet_count = 3;

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

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

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

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

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

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

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

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

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

853 854 855 856 857 858
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];
859
        if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
860
            sc->first_gop_closed= (buf[i+4]>>6)&1;
861 862 863 864
    }
    return (buf[i+1]>>3)&7;
}

865
static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
866
{
867
    GXFContext *gxf = s->priv_data;
868
    AVIOContext *pb = s->pb;
869 870
    AVStream *st = s->streams[pkt->stream_index];
    GXFStreamContext *sc = st->priv_data;
871 872 873 874
    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 */
875
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
876
        field_nb = gxf->nb_fields;
877
    } else {
878 879
        field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
                                  (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
880
    }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
881

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

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

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

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

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

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

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

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

957 958 959 960 961 962 963 964 965 966
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;
967
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
968 969 970 971 972 973 974 975 976 977 978
            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
979 980
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
{
981
    if (pkt && s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
982
        pkt->duration = 2; // enforce 2 fields
983
    return ff_audio_rechunk_interleave(s, out, pkt, flush,
984
                               ff_interleave_packet_per_dts, gxf_compare_field_nb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
985 986
}

987
AVOutputFormat ff_gxf_muxer = {
988 989 990 991 992 993 994 995 996 997
    .name              = "gxf",
    .long_name         = NULL_IF_CONFIG_SMALL("GXF format"),
    .extensions        = "gxf",
    .priv_data_size    = sizeof(GXFContext),
    .audio_codec       = CODEC_ID_PCM_S16LE,
    .video_codec       = CODEC_ID_MPEG2VIDEO,
    .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
998
};