gxfenc.c 31.8 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
        if (st->codecpar->height == gxf_lines_tab[i].height) {
128
            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->codecpar->height == 512 || st->codecpar->height == 608)
202
        starting_line = 7; // VBI
203
    else if (st->codecpar->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->codecpar->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
211
                    st->codecpar->format == AV_PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
212
                    starting_line, (st->codecpar->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 221 222 223 224 225
static int gxf_write_dv_auxiliary(AVIOContext *pb, AVStream *st)
{
    int64_t track_aux_data = 0;

    avio_w8(pb, TRACK_AUX);
    avio_w8(pb, 8);
226
    if (st->codecpar->format == AV_PIX_FMT_YUV420P)
227 228 229 230 231 232
        track_aux_data |= 0x01;     /* marks stream as DVCAM instead of DVPRO */
    track_aux_data |= 0x40000000;   /* aux data is valid */
    avio_wl64(pb, track_aux_data);
    return 8;
}

233
static int gxf_write_timecode_auxiliary(AVIOContext *pb, GXFContext *gxf)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
234
{
235 236 237 238
    uint32_t timecode = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
                                     gxf->tc.hh, gxf->tc.mm,
                                     gxf->tc.ss, gxf->tc.ff);

239 240
    avio_w8(pb, TRACK_AUX);
    avio_w8(pb, 8);
241
    avio_wl32(pb, timecode);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
242
    /* reserved */
243
    avio_wl32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
244 245 246
    return 8;
}

247
static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
248
{
249
    GXFContext *gxf = s->priv_data;
250
    AVIOContext *pb = s->pb;
251
    int64_t pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
252 253

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

257
    pos = avio_tell(pb);
258
    avio_wb16(pb, 0); /* size */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
259 260

    /* media file name */
261 262
    avio_w8(pb, TRACK_NAME);
    avio_w8(pb, strlen(ES_NAME_PATTERN) + 3);
263
    avio_write(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN) - 1);
264 265
    avio_wb16(pb, sc->media_info);
    avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
266

267 268
    switch (sc->track_type) {
        case 3:     /* timecode */
269
            gxf_write_timecode_auxiliary(pb, gxf);
270 271 272 273 274 275 276 277 278 279 280 281
            break;
        case 4:     /* MPEG2 */
        case 9:     /* MPEG1 */
            gxf_write_mpeg_auxiliary(pb, s->streams[index]);
            break;
        case 5:     /* DV25 */
        case 6:     /* DV50 */
            gxf_write_dv_auxiliary(pb, s->streams[index]);
            break;
        default:
            avio_w8(pb, TRACK_AUX);
            avio_w8(pb, 8);
282
            avio_wl64(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
283 284 285
    }

    /* file system version */
286 287 288
    avio_w8(pb, TRACK_VER);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
289 290

    /* frame rate */
291 292 293
    avio_w8(pb, TRACK_FPS);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->frame_rate_index);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
294 295

    /* lines per frame */
296 297 298
    avio_w8(pb, TRACK_LINES);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->lines_index);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
299 300

    /* fields per frame */
301 302 303
    avio_w8(pb, TRACK_FPF);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
304 305 306 307

    return updateSize(pb, pos);
}

308
static int gxf_write_material_data_section(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
309
{
310
    GXFContext *gxf = s->priv_data;
311
    AVIOContext *pb = s->pb;
312
    int64_t pos;
313
    int len;
314
    const char *filename = strrchr(s->filename, '/');
Baptiste Coudurier's avatar
Baptiste Coudurier committed
315

316
    pos = avio_tell(pb);
317
    avio_wb16(pb, 0); /* size */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
318 319 320 321 322

    /* name */
    if (filename)
        filename++;
    else
323
        filename = s->filename;
324 325
    len = strlen(filename);

326
    avio_w8(pb, MAT_NAME);
327 328 329
    avio_w8(pb, strlen(SERVER_PATH) + len + 1);
    avio_write(pb, SERVER_PATH, sizeof(SERVER_PATH) - 1);
    avio_write(pb, filename, len);
330
    avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
331 332

    /* first field */
333 334 335
    avio_w8(pb, MAT_FIRST_FIELD);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
336 337

    /* last field */
338 339 340
    avio_w8(pb, MAT_LAST_FIELD);
    avio_w8(pb, 4);
    avio_wb32(pb, gxf->nb_fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
341 342

    /* reserved */
343 344 345
    avio_w8(pb, MAT_MARK_IN);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
346

347 348 349
    avio_w8(pb, MAT_MARK_OUT);
    avio_w8(pb, 4);
    avio_wb32(pb, gxf->nb_fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
350 351

    /* estimated size */
352 353
    avio_w8(pb, MAT_SIZE);
    avio_w8(pb, 4);
354
    avio_wb32(pb, avio_size(pb) / 1024);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
355 356 357 358

    return updateSize(pb, pos);
}

359
static int gxf_write_track_description_section(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
360
{
361
    GXFContext *gxf = s->priv_data;
362
    AVIOContext *pb = s->pb;
363
    int64_t pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
364 365
    int i;

366
    pos = avio_tell(pb);
367
    avio_wb16(pb, 0); /* size */
368
    for (i = 0; i < s->nb_streams; ++i)
369 370 371 372
        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
373 374 375
    return updateSize(pb, pos);
}

376
static int gxf_write_map_packet(AVFormatContext *s, int rewrite)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
377
{
378
    GXFContext *gxf = s->priv_data;
379
    AVIOContext *pb = s->pb;
380
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
381

382 383
    if (!rewrite) {
        if (!(gxf->map_offsets_nb % 30)) {
384 385 386 387 388
            int err;
            if ((err = av_reallocp_array(&gxf->map_offsets,
                                         gxf->map_offsets_nb + 30,
                                         sizeof(*gxf->map_offsets))) < 0) {
                gxf->map_offsets_nb = 0;
389
                av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n");
390
                return err;
391 392 393 394 395
            }
        }
        gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here
    }

Baptiste Coudurier's avatar
Baptiste Coudurier committed
396 397 398
    gxf_write_packet_header(pb, PKT_MAP);

    /* preamble */
399 400
    avio_w8(pb, 0xE0); /* version */
    avio_w8(pb, 0xFF); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
401

402 403
    gxf_write_material_data_section(s);
    gxf_write_track_description_section(s);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
404 405 406 407

    return updatePacketSize(pb, pos);
}

408
static int gxf_write_flt_packet(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
409
{
410
    GXFContext *gxf = s->priv_data;
411
    AVIOContext *pb = s->pb;
412
    int64_t pos = avio_tell(pb);
413
    int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
414
    int flt_entries = gxf->nb_fields / fields_per_flt;
415
    int i = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
416 417 418

    gxf_write_packet_header(pb, PKT_FLT);

419 420
    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
421

422 423
    if (gxf->flt_entries) {
        for (i = 0; i < flt_entries; i++)
424
            avio_wl32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
425
    }
426 427

    for (; i < 1000; i++)
428
        avio_wl32(pb, 0);
429

Baptiste Coudurier's avatar
Baptiste Coudurier committed
430 431 432
    return updatePacketSize(pb, pos);
}

433
static int gxf_write_umf_material_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
434
{
435
    GXFContext *gxf = s->priv_data;
436
    AVIOContext *pb = s->pb;
437
    int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
438
    int64_t timestamp = 0;
439 440 441
    uint64_t nb_fields;
    uint32_t timecode_in; // timecode at mark in
    uint32_t timecode_out; // timecode at mark out
442

443
    ff_parse_creation_time_metadata(s, &timestamp, 1);
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    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);
460

461 462 463 464 465
    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 */
466 467
    avio_wl32(pb, timecode_in); /* timecode mark in */
    avio_wl32(pb, timecode_out); /* timecode mark out */
468 469
    avio_wl64(pb, timestamp); /* modification time */
    avio_wl64(pb, timestamp); /* creation time */
470 471 472 473 474 475
    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
476 477 478
    return 48;
}

479
static int gxf_write_umf_payload(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
480
{
481
    GXFContext *gxf = s->priv_data;
482
    AVIOContext *pb = s->pb;
483

484 485 486 487 488 489 490 491 492 493 494 495
    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
496 497 498
    return 48;
}

499
static int gxf_write_umf_track_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
500
{
501
    AVIOContext *pb = s->pb;
502
    GXFContext *gxf = s->priv_data;
503
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
504 505
    int i;

506 507
    gxf->umf_track_offset = pos - gxf->umf_start_offset;
    for (i = 0; i < s->nb_streams; ++i) {
508
        GXFStreamContext *sc = s->streams[i]->priv_data;
509 510
        avio_wl16(pb, sc->media_info);
        avio_wl16(pb, 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
511
    }
512

513 514
    avio_wl16(pb, gxf->timecode_track.media_info);
    avio_wl16(pb, 1);
515

516
    return avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
517 518
}

519
static int gxf_write_umf_media_mpeg(AVIOContext *pb, AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
520
{
521 522
    GXFStreamContext *sc = st->priv_data;

523
    if (st->codecpar->format == AV_PIX_FMT_YUV422P)
524
        avio_wl32(pb, 2);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
525
    else
526 527 528 529 530 531
        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);
532
    if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
533
        avio_wl32(pb, 2);
534
    else if (st->codecpar->codec_id == AV_CODEC_ID_MPEG1VIDEO)
535
        avio_wl32(pb, 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
536
    else
537 538
        avio_wl32(pb, 0);
    avio_wl32(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
539 540 541
    return 32;
}

542
static int gxf_write_umf_media_timecode(AVIOContext *pb, int drop)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
543
{
544
    avio_wl32(pb, drop); /* drop frame */
545 546 547 548 549 550 551
    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
552 553 554
    return 32;
}

555
static int gxf_write_umf_media_dv(AVIOContext *pb, GXFStreamContext *sc, AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
556
{
557
    int dv_umf_data = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
558

559
    if (st->codecpar->format == AV_PIX_FMT_YUV420P)
560 561 562 563 564 565 566 567 568
        dv_umf_data |= 0x20; /* marks as DVCAM instead of DVPRO */
    avio_wl32(pb, dv_umf_data);
    avio_wl32(pb, 0);
    avio_wl32(pb, 0);
    avio_wl32(pb, 0);
    avio_wl32(pb, 0);
    avio_wl32(pb, 0);
    avio_wl32(pb, 0);
    avio_wl32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
569 570 571
    return 32;
}

572
static int gxf_write_umf_media_audio(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
573
{
574 575
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
576 577 578 579
    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
580 581 582
    return 32;
}

583
static int gxf_write_umf_media_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
584
{
585
    GXFContext *gxf = s->priv_data;
586
    AVIOContext *pb = s->pb;
587
    int64_t pos;
588
    int i, j;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
589

590
    pos = avio_tell(pb);
591
    gxf->umf_media_offset = pos - gxf->umf_start_offset;
592 593
    for (i = 0; i <= s->nb_streams; ++i) {
        GXFStreamContext *sc;
594
        int64_t startpos, curpos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
595

596 597 598 599 600
        if (i == s->nb_streams)
            sc = &gxf->timecode_track;
        else
            sc = s->streams[i]->priv_data;

601
        startpos = avio_tell(pb);
602 603 604 605 606 607 608 609 610 611
        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);
612
        for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
613 614 615 616 617
            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 */
618 619

        if (sc == &gxf->timecode_track)
620
            gxf_write_umf_media_timecode(pb, gxf->tc.drop);
621 622
        else {
            AVStream *st = s->streams[i];
623
            switch (st->codecpar->codec_id) {
624 625
            case AV_CODEC_ID_MPEG1VIDEO:
            case AV_CODEC_ID_MPEG2VIDEO:
Baptiste Coudurier's avatar
Baptiste Coudurier committed
626 627
                gxf_write_umf_media_mpeg(pb, st);
                break;
628
            case AV_CODEC_ID_PCM_S16LE:
Baptiste Coudurier's avatar
Baptiste Coudurier committed
629 630
                gxf_write_umf_media_audio(pb, sc);
                break;
631
            case AV_CODEC_ID_DVVIDEO:
632
                gxf_write_umf_media_dv(pb, sc, st);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
633 634
                break;
            }
635 636
        }

637
        curpos = avio_tell(pb);
638
        avio_seek(pb, startpos, SEEK_SET);
639
        avio_wl16(pb, curpos - startpos);
640
        avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
641
    }
642
    return avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
643 644
}

645
static int gxf_write_umf_packet(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
646
{
647
    GXFContext *gxf = s->priv_data;
648
    AVIOContext *pb = s->pb;
649
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
650 651 652 653

    gxf_write_packet_header(pb, PKT_UMF);

    /* preamble */
654 655
    avio_w8(pb, 3); /* first and last (only) packet */
    avio_wb32(pb, gxf->umf_length); /* data length */
656

657
    gxf->umf_start_offset = avio_tell(pb);
658 659 660 661
    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);
662
    gxf->umf_length = avio_tell(pb) - gxf->umf_start_offset;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
663 664 665
    return updatePacketSize(pb, pos);
}

666 667
static const int GXF_samples_per_frame[] = { 32768, 0 };

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
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;
}

683
static int gxf_init_timecode(AVFormatContext *s, GXFTimecode *tc, const char *tcstr, int fields)
684 685 686
{
    char c;

687
    if (sscanf(tcstr, "%d:%d:%d%c%d", &tc->hh, &tc->mm, &tc->ss, &c, &tc->ff) != 5) {
688 689 690 691 692 693 694 695 696 697 698 699 700 701
        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
702 703
static int gxf_write_header(AVFormatContext *s)
{
704
    AVIOContext *pb = s->pb;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
705
    GXFContext *gxf = s->priv_data;
706
    GXFStreamContext *vsc = NULL;
707 708
    uint8_t tracks[255] = {0};
    int i, media_info = 0;
709
    int ret;
710
    AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
711

712
    if (!pb->seekable) {
713
        av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome\n");
714 715 716
        return -1;
    }

Baptiste Coudurier's avatar
Baptiste Coudurier committed
717 718 719
    gxf->flags |= 0x00080000; /* material is simple clip */
    for (i = 0; i < s->nb_streams; ++i) {
        AVStream *st = s->streams[i];
720 721 722 723
        GXFStreamContext *sc = av_mallocz(sizeof(*sc));
        if (!sc)
            return AVERROR(ENOMEM);
        st->priv_data = sc;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
724

725 726 727
        sc->media_type = ff_codec_get_tag(gxf_media_types, st->codecpar->codec_id);
        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
            if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
728 729 730
                av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
                return -1;
            }
731
            if (st->codecpar->sample_rate != 48000) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
732 733 734
                av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
                return -1;
            }
735
            if (st->codecpar->channels != 1) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
736 737 738 739
                av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
                return -1;
            }
            sc->track_type = 2;
740
            sc->sample_rate = st->codecpar->sample_rate;
741
            avpriv_set_pts_info(st, 64, 1, sc->sample_rate);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
742 743 744 745 746 747
            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 */
748
            media_info = 'A';
749
        } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
750 751 752 753
            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
754
            /* FIXME check from time_base ? */
755
            if (st->codecpar->height == 480 || st->codecpar->height == 512) { /* NTSC or NTSC+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
756 757 758
                sc->frame_rate_index = 5;
                sc->sample_rate = 60;
                gxf->flags |= 0x00000080;
759
                gxf->time_base = (AVRational){ 1001, 60000 };
760
            } else if (st->codecpar->height == 576 || st->codecpar->height == 608) { /* PAL or PAL+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
761 762 763 764
                sc->frame_rate_index = 6;
                sc->media_type++;
                sc->sample_rate = 50;
                gxf->flags |= 0x00000040;
765
                gxf->time_base = (AVRational){ 1, 50 };
766 767 768 769
            } 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
770
            }
771 772
            if (!tcr)
                tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
773
            avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
774
            if (gxf_find_lines_index(st) < 0)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
775
                sc->lines_index = -1;
776
            sc->sample_size = st->codecpar->bit_rate;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
777
            sc->fields = 2; /* interlaced */
778 779 780

            vsc = sc;

781
            switch (st->codecpar->codec_id) {
782
            case AV_CODEC_ID_MJPEG:
783 784 785 786
                sc->track_type = 1;
                gxf->flags |= 0x00004000;
                media_info = 'J';
                break;
787
            case AV_CODEC_ID_MPEG1VIDEO:
788 789 790 791
                sc->track_type = 9;
                gxf->mpeg_tracks++;
                media_info = 'L';
                break;
792
            case AV_CODEC_ID_MPEG2VIDEO:
793
                sc->first_gop_closed = -1;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
794 795 796
                sc->track_type = 4;
                gxf->mpeg_tracks++;
                gxf->flags |= 0x00008000;
797
                media_info = 'M';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
798
                break;
799
            case AV_CODEC_ID_DVVIDEO:
800
                if (st->codecpar->format == AV_PIX_FMT_YUV422P) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
801 802 803
                    sc->media_type += 2;
                    sc->track_type = 6;
                    gxf->flags |= 0x00002000;
804
                    media_info = 'E';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
805 806 807
                } else {
                    sc->track_type = 5;
                    gxf->flags |= 0x00001000;
808
                    media_info = 'D';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
809 810 811
                }
                break;
            default:
812
                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
813 814 815
                return -1;
            }
        }
816 817
        /* 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]++);
818
        sc->order = s->nb_streams - st->index;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
819
    }
820 821 822 823

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

824
    if (tcr && vsc)
825
        gxf_init_timecode(s, &gxf->tc, tcr->value, vsc->fields);
826

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

830 831
    if ((ret = gxf_write_map_packet(s, 0)) < 0)
        return ret;
832
    gxf_write_flt_packet(s);
833
    gxf_write_umf_packet(s);
834 835 836

    gxf->packet_count = 3;

837
    avio_flush(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
838 839 840
    return 0;
}

841
static int gxf_write_eos_packet(AVIOContext *pb)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
842
{
843
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
844 845 846 847 848 849 850

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

static int gxf_write_trailer(AVFormatContext *s)
{
851
    GXFContext *gxf = s->priv_data;
852
    AVIOContext *pb = s->pb;
853
    int64_t end;
854
    int i;
855
    int ret;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
856

857
    ff_audio_interleave_close(s);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
858

859
    gxf_write_eos_packet(pb);
860
    end = avio_tell(pb);
861
    avio_seek(pb, 0, SEEK_SET);
862
    /* overwrite map, flt and umf packets with new values */
863 864
    if ((ret = gxf_write_map_packet(s, 1)) < 0)
        return ret;
865
    gxf_write_flt_packet(s);
866
    gxf_write_umf_packet(s);
867
    avio_flush(pb);
868 869
    /* update duration in all map packets */
    for (i = 1; i < gxf->map_offsets_nb; i++) {
870
        avio_seek(pb, gxf->map_offsets[i], SEEK_SET);
871 872
        if ((ret = gxf_write_map_packet(s, 1)) < 0)
            return ret;
873
        avio_flush(pb);
874 875
    }

876
    avio_seek(pb, end, SEEK_SET);
877 878

    av_freep(&gxf->flt_entries);
879
    av_freep(&gxf->map_offsets);
880

Baptiste Coudurier's avatar
Baptiste Coudurier committed
881 882 883
    return 0;
}

884 885 886 887 888 889
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];
890
        if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
891
            sc->first_gop_closed= (buf[i+4]>>6)&1;
892 893 894 895
    }
    return (buf[i+1]>>3)&7;
}

896
static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
897
{
898
    GXFContext *gxf = s->priv_data;
899
    AVIOContext *pb = s->pb;
900 901
    AVStream *st = s->streams[pkt->stream_index];
    GXFStreamContext *sc = st->priv_data;
902 903 904 905
    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 */
906
    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
907
        field_nb = gxf->nb_fields;
908
    } else {
909 910
        field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
                                  (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
911
    }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
912

913 914 915
    avio_w8(pb, sc->media_type);
    avio_w8(pb, st->index);
    avio_wb32(pb, field_nb);
916
    if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
917 918
        avio_wb16(pb, 0);
        avio_wb16(pb, size / 2);
919
    } else if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
920
        int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
921
        if (frame_type == AV_PICTURE_TYPE_I) {
922
            avio_w8(pb, 0x0d);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
923
            sc->iframes++;
924
        } else if (frame_type == AV_PICTURE_TYPE_B) {
925
            avio_w8(pb, 0x0f);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
926 927
            sc->bframes++;
        } else {
928
            avio_w8(pb, 0x0e);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
929 930
            sc->pframes++;
        }
931
        avio_wb24(pb, size);
932
    } else if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
933 934
        avio_w8(pb, size / 4096);
        avio_wb24(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
935
    } else
936 937 938 939
        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
940 941 942
    return 16;
}

943
static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
944
{
945
    GXFContext *gxf = s->priv_data;
946
    AVIOContext *pb = s->pb;
947
    AVStream *st = s->streams[pkt->stream_index];
948
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
949
    int padding = 0;
950
    unsigned packet_start_offset = avio_tell(pb) / 1024;
951
    int ret;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
952 953

    gxf_write_packet_header(pb, PKT_MEDIA);
954
    if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
955
        padding = 4 - pkt->size % 4;
956
    else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
957
        padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
958
    gxf_write_media_preamble(s, pkt, pkt->size + padding);
959
    avio_write(pb, pkt->data, pkt->size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
960
    gxf_write_padding(pb, padding);
961

962
    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
963
        if (!(gxf->flt_entries_nb % 500)) {
964 965 966 967 968
            int err;
            if ((err = av_reallocp_array(&gxf->flt_entries,
                                         gxf->flt_entries_nb + 500,
                                         sizeof(*gxf->flt_entries))) < 0) {
                gxf->flt_entries_nb = 0;
969
                gxf->nb_fields = 0;
970
                av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
971
                return err;
972 973
            }
        }
974
        gxf->flt_entries[gxf->flt_entries_nb++] = packet_start_offset;
975
        gxf->nb_fields += 2; // count fields
976
    }
977

978 979 980 981
    updatePacketSize(pb, pos);

    gxf->packet_count++;
    if (gxf->packet_count == 100) {
982 983
        if ((ret = gxf_write_map_packet(s, 0)) < 0)
            return ret;
984 985 986 987
        gxf->packet_count = 0;
    }

    return 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
988 989
}

990 991 992 993 994 995 996 997 998 999
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;
1000
        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
            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
1012 1013
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
{
1014
    if (pkt && s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1015
        pkt->duration = 2; // enforce 2 fields
1016
    return ff_audio_rechunk_interleave(s, out, pkt, flush,
1017
                               ff_interleave_packet_per_dts, gxf_compare_field_nb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
1018 1019
}

1020
AVOutputFormat ff_gxf_muxer = {
1021
    .name              = "gxf",
1022
    .long_name         = NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"),
1023 1024
    .extensions        = "gxf",
    .priv_data_size    = sizeof(GXFContext),
1025 1026
    .audio_codec       = AV_CODEC_ID_PCM_S16LE,
    .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
1027 1028 1029 1030
    .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
1031
};