gxfenc.c 29.2 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
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10
 * 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
 * Libav 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
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; 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/mathematics.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
24
#include "avformat.h"
25
#include "internal.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
26 27
#include "gxf.h"
#include "riff.h"
28
#include "audiointerleave.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
29 30 31 32

#define GXF_AUDIO_PACKET_SIZE 65536

typedef struct GXFStreamContext {
33
    AudioInterleaveContext aic;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
34 35 36 37 38 39 40 41 42 43 44 45
    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;
46
    int b_per_i_or_p; ///< number of B frames per I frame or P frame
47
    int first_gop_closed;
48
    unsigned order;   ///< interleaving order
Baptiste Coudurier's avatar
Baptiste Coudurier committed
49 50 51
} GXFStreamContext;

typedef struct GXFContext {
52
    uint32_t nb_fields;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
53 54 55 56 57 58 59 60 61
    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;
62
    AVRational time_base;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
63
    int flags;
64
    GXFStreamContext timecode_track;
65 66
    unsigned *flt_entries;    ///< offsets of packets /1024, starts after 2nd video field
    unsigned flt_entries_nb;
67 68 69
    uint64_t *map_offsets;    ///< offset of map packets
    unsigned map_offsets_nb;
    unsigned packet_count;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
70 71
} GXFContext;

72 73 74
static const struct {
    int height, index;
} gxf_lines_tab[] = {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
75 76 77 78 79 80 81 82
    { 480,  1 }, /* NTSC */
    { 512,  1 }, /* NTSC + VBI */
    { 576,  2 }, /* PAL */
    { 608,  2 }, /* PAL + VBI */
    { 1080, 4 },
    { 720,  6 },
};

83
static const AVCodecTag gxf_media_types[] = {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    { 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 */
99
    { CODEC_ID_NONE,         0 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
100 101
};

102 103
#define SERVER_PATH "EXT:/PDR/default/"
#define ES_NAME_PATTERN "EXT:/PDR/default/ES."
Baptiste Coudurier's avatar
Baptiste Coudurier committed
104

105
static int gxf_find_lines_index(AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
106
{
107
    GXFStreamContext *sc = st->priv_data;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
108 109 110
    int i;

    for (i = 0; i < 6; ++i) {
111 112
        if (st->codec->height == gxf_lines_tab[i].height) {
            sc->lines_index = gxf_lines_tab[i].index;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
113 114 115 116 117 118
            return 0;
        }
    }
    return -1;
}

119
static void gxf_write_padding(AVIOContext *pb, int64_t to_pad)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
120
{
121
    for (; to_pad > 0; to_pad--) {
122
        avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
123 124 125
    }
}

126
static int64_t updatePacketSize(AVIOContext *pb, int64_t pos)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
127
{
128
    int64_t curpos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
129 130
    int size;

131
    size = avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
132 133
    if (size % 4) {
        gxf_write_padding(pb, 4 - size % 4);
134
        size = avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
135
    }
136
    curpos = avio_tell(pb);
137
    avio_seek(pb, pos + 6, SEEK_SET);
138
    avio_wb32(pb, size);
139
    avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
140 141 142
    return curpos - pos;
}

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

147
    curpos = avio_tell(pb);
148
    avio_seek(pb, pos, SEEK_SET);
149
    avio_wb16(pb, curpos - pos - 2);
150
    avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
151 152 153
    return curpos - pos;
}

154
static void gxf_write_packet_header(AVIOContext *pb, GXFPktType type)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
155
{
156 157 158 159 160 161 162
    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
163 164
}

165
static int gxf_write_mpeg_auxiliary(AVIOContext *pb, AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
166
{
167
    GXFStreamContext *sc = st->priv_data;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
168
    char buffer[1024];
169
    int size, starting_line;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
170

171 172 173 174 175 176 177 178
    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++;
179
        }
180 181 182 183
        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
184
    }
185
    if (st->codec->height == 512 || st->codec->height == 608)
186
        starting_line = 7; // VBI
187
    else if (st->codec->height == 480)
188 189 190 191
        starting_line = 20;
    else
        starting_line = 23; // default PAL

Baptiste Coudurier's avatar
Baptiste Coudurier committed
192
    size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
193
                    "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
194 195
                    (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,
196
                    starting_line, (st->codec->height + 15) / 16);
197 198 199
    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
200 201 202
    return size + 3;
}

203
static int gxf_write_timecode_auxiliary(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
204
{
205 206 207 208
    avio_w8(pb, 0); /* fields */
    avio_w8(pb, 0); /* seconds */
    avio_w8(pb, 0); /* minutes */
    avio_w8(pb, 0); /* flags + hours */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
209
    /* reserved */
210
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
211 212 213
    return 8;
}

214
static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
215
{
216
    AVIOContext *pb = s->pb;
217
    int64_t pos;
218
    int mpeg = sc->track_type == 4 || sc->track_type == 9;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
219 220

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

224
    pos = avio_tell(pb);
225
    avio_wb16(pb, 0); /* size */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
226 227

    /* media file name */
228 229
    avio_w8(pb, TRACK_NAME);
    avio_w8(pb, strlen(ES_NAME_PATTERN) + 3);
230
    avio_write(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN) - 1);
231 232
    avio_wb16(pb, sc->media_info);
    avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
233

234
    if (!mpeg) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
235
        /* auxiliary information */
236 237
        avio_w8(pb, TRACK_AUX);
        avio_w8(pb, 8);
238
        if (sc->track_type == 3)
239
            gxf_write_timecode_auxiliary(pb, sc);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
240
        else
241
            avio_wl64(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
242 243 244
    }

    /* file system version */
245 246 247
    avio_w8(pb, TRACK_VER);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
248

249 250
    if (mpeg)
        gxf_write_mpeg_auxiliary(pb, s->streams[index]);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
251 252

    /* frame rate */
253 254 255
    avio_w8(pb, TRACK_FPS);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->frame_rate_index);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
256 257

    /* lines per frame */
258 259 260
    avio_w8(pb, TRACK_LINES);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->lines_index);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
261 262

    /* fields per frame */
263 264 265
    avio_w8(pb, TRACK_FPF);
    avio_w8(pb, 4);
    avio_wb32(pb, sc->fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
266 267 268 269

    return updateSize(pb, pos);
}

270
static int gxf_write_material_data_section(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
271
{
272
    GXFContext *gxf = s->priv_data;
273
    AVIOContext *pb = s->pb;
274
    int64_t pos;
275
    int len;
276
    const char *filename = strrchr(s->filename, '/');
Baptiste Coudurier's avatar
Baptiste Coudurier committed
277

278
    pos = avio_tell(pb);
279
    avio_wb16(pb, 0); /* size */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
280 281 282 283 284

    /* name */
    if (filename)
        filename++;
    else
285
        filename = s->filename;
286 287
    len = strlen(filename);

288
    avio_w8(pb, MAT_NAME);
289 290 291
    avio_w8(pb, strlen(SERVER_PATH) + len + 1);
    avio_write(pb, SERVER_PATH, sizeof(SERVER_PATH) - 1);
    avio_write(pb, filename, len);
292
    avio_w8(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
293 294

    /* first field */
295 296 297
    avio_w8(pb, MAT_FIRST_FIELD);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
298 299

    /* last field */
300 301 302
    avio_w8(pb, MAT_LAST_FIELD);
    avio_w8(pb, 4);
    avio_wb32(pb, gxf->nb_fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
303 304

    /* reserved */
305 306 307
    avio_w8(pb, MAT_MARK_IN);
    avio_w8(pb, 4);
    avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
308

309 310 311
    avio_w8(pb, MAT_MARK_OUT);
    avio_w8(pb, 4);
    avio_wb32(pb, gxf->nb_fields);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
312 313

    /* estimated size */
314 315
    avio_w8(pb, MAT_SIZE);
    avio_w8(pb, 4);
316
    avio_wb32(pb, avio_size(pb) / 1024);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
317 318 319 320

    return updateSize(pb, pos);
}

321
static int gxf_write_track_description_section(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
322
{
323
    GXFContext *gxf = s->priv_data;
324
    AVIOContext *pb = s->pb;
325
    int64_t pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
326 327
    int i;

328
    pos = avio_tell(pb);
329
    avio_wb16(pb, 0); /* size */
330
    for (i = 0; i < s->nb_streams; ++i)
331 332 333 334
        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
335 336 337
    return updateSize(pb, pos);
}

338
static int gxf_write_map_packet(AVFormatContext *s, int rewrite)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
339
{
340
    GXFContext *gxf = s->priv_data;
341
    AVIOContext *pb = s->pb;
342
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
343

344 345 346 347 348 349 350 351 352 353 354 355
    if (!rewrite) {
        if (!(gxf->map_offsets_nb % 30)) {
            gxf->map_offsets = av_realloc(gxf->map_offsets,
                                          (gxf->map_offsets_nb+30)*sizeof(*gxf->map_offsets));
            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
356 357 358
    gxf_write_packet_header(pb, PKT_MAP);

    /* preamble */
359 360
    avio_w8(pb, 0xE0); /* version */
    avio_w8(pb, 0xFF); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
361

362 363
    gxf_write_material_data_section(s);
    gxf_write_track_description_section(s);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
364 365 366 367

    return updatePacketSize(pb, pos);
}

368
static int gxf_write_flt_packet(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
369
{
370
    GXFContext *gxf = s->priv_data;
371
    AVIOContext *pb = s->pb;
372
    int64_t pos = avio_tell(pb);
373
    int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
374
    int flt_entries = gxf->nb_fields / fields_per_flt;
375
    int i = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
376 377 378

    gxf_write_packet_header(pb, PKT_FLT);

379 380
    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
381

382 383
    if (gxf->flt_entries) {
        for (i = 0; i < flt_entries; i++)
384
            avio_wl32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
385
    }
386 387

    for (; i < 1000; i++)
388
        avio_wl32(pb, 0);
389

Baptiste Coudurier's avatar
Baptiste Coudurier committed
390 391 392
    return updatePacketSize(pb, pos);
}

393
static int gxf_write_umf_material_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
394
{
395
    GXFContext *gxf = s->priv_data;
396
    AVIOContext *pb = s->pb;
397
    int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
398 399
    int64_t timestamp = 0;
    AVDictionaryEntry *t;
400
    uint32_t timecode;
401 402 403 404 405 406

#if FF_API_TIMESTAMP
    if (s->timestamp)
        timestamp = s->timestamp;
    else
#endif
407 408
    if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
        timestamp = ff_iso8601_to_unix_time(t->value);
409

410
    // XXX drop frame
411
    timecode =
412 413 414 415
        gxf->nb_fields / (timecode_base * 3600) % 24 << 24 | // hours
        gxf->nb_fields / (timecode_base * 60) % 60   << 16 | // minutes
        gxf->nb_fields /  timecode_base % 60         <<  8 | // seconds
        gxf->nb_fields %  timecode_base;                     // fields
416

417 418 419 420 421 422 423
    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 */
    avio_wl32(pb, 0); /* timecode mark in */
    avio_wl32(pb, timecode); /* timecode mark out */
424 425
    avio_wl64(pb, timestamp); /* modification time */
    avio_wl64(pb, timestamp); /* creation time */
426 427 428 429 430 431
    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
432 433 434
    return 48;
}

435
static int gxf_write_umf_payload(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
436
{
437
    GXFContext *gxf = s->priv_data;
438
    AVIOContext *pb = s->pb;
439

440 441 442 443 444 445 446 447 448 449 450 451
    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
452 453 454
    return 48;
}

455
static int gxf_write_umf_track_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
456
{
457
    AVIOContext *pb = s->pb;
458
    GXFContext *gxf = s->priv_data;
459
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
460 461
    int i;

462 463
    gxf->umf_track_offset = pos - gxf->umf_start_offset;
    for (i = 0; i < s->nb_streams; ++i) {
464
        GXFStreamContext *sc = s->streams[i]->priv_data;
465 466
        avio_wl16(pb, sc->media_info);
        avio_wl16(pb, 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
467
    }
468

469 470
    avio_wl16(pb, gxf->timecode_track.media_info);
    avio_wl16(pb, 1);
471

472
    return avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
473 474
}

475
static int gxf_write_umf_media_mpeg(AVIOContext *pb, AVStream *st)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
476
{
477 478 479
    GXFStreamContext *sc = st->priv_data;

    if (st->codec->pix_fmt == PIX_FMT_YUV422P)
480
        avio_wl32(pb, 2);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
481
    else
482 483 484 485 486 487
        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);
488
    if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
489
        avio_wl32(pb, 2);
490
    else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO)
491
        avio_wl32(pb, 1);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
492
    else
493 494
        avio_wl32(pb, 0);
    avio_wl32(pb, 0); /* reserved */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
495 496 497
    return 32;
}

498
static int gxf_write_umf_media_timecode(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
499
{
500 501 502 503 504 505 506 507
    avio_wl32(pb, 1); /* non drop frame */
    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
508 509 510
    return 32;
}

511
static int gxf_write_umf_media_dv(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
512 513 514 515
{
    int i;

    for (i = 0; i < 8; i++) {
516
        avio_wb32(pb, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
517 518 519 520
    }
    return 32;
}

521
static int gxf_write_umf_media_audio(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
522
{
523 524
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
    avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
525 526 527 528
    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
529 530 531 532
    return 32;
}

#if 0
533
static int gxf_write_umf_media_mjpeg(AVIOContext *pb, GXFStreamContext *sc)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
534
{
535 536 537 538
    avio_wb64(pb, 0); /* FIXME FLOAT max chroma quant level */
    avio_wb64(pb, 0); /* FIXME FLOAT max luma quant level */
    avio_wb64(pb, 0); /* FIXME FLOAT min chroma quant level */
    avio_wb64(pb, 0); /* FIXME FLOAT min luma quant level */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
539 540 541 542
    return 32;
}
#endif

543
static int gxf_write_umf_media_description(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
544
{
545
    GXFContext *gxf = s->priv_data;
546
    AVIOContext *pb = s->pb;
547
    int64_t pos;
548
    int i, j;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
549

550
    pos = avio_tell(pb);
551
    gxf->umf_media_offset = pos - gxf->umf_start_offset;
552 553
    for (i = 0; i <= s->nb_streams; ++i) {
        GXFStreamContext *sc;
554
        int64_t startpos, curpos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
555

556 557 558 559 560
        if (i == s->nb_streams)
            sc = &gxf->timecode_track;
        else
            sc = s->streams[i]->priv_data;

561
        startpos = avio_tell(pb);
562 563 564 565 566 567 568 569 570 571
        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);
572
        for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
573 574 575 576 577
            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 */
578 579 580 581 582

        if (sc == &gxf->timecode_track)
            gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
        else {
            AVStream *st = s->streams[i];
Baptiste Coudurier's avatar
Baptiste Coudurier committed
583
            switch (st->codec->codec_id) {
584
            case CODEC_ID_MPEG1VIDEO:
Baptiste Coudurier's avatar
Baptiste Coudurier committed
585 586 587 588 589 590 591 592 593 594
            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;
            }
595 596
        }

597
        curpos = avio_tell(pb);
598
        avio_seek(pb, startpos, SEEK_SET);
599
        avio_wl16(pb, curpos - startpos);
600
        avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
601
    }
602
    return avio_tell(pb) - pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
603 604
}

605
static int gxf_write_umf_packet(AVFormatContext *s)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
606
{
607
    GXFContext *gxf = s->priv_data;
608
    AVIOContext *pb = s->pb;
609
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
610 611 612 613

    gxf_write_packet_header(pb, PKT_UMF);

    /* preamble */
614 615
    avio_w8(pb, 3); /* first and last (only) packet */
    avio_wb32(pb, gxf->umf_length); /* data length */
616

617
    gxf->umf_start_offset = avio_tell(pb);
618 619 620 621
    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);
622
    gxf->umf_length = avio_tell(pb) - gxf->umf_start_offset;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
623 624 625
    return updatePacketSize(pb, pos);
}

626 627
static const int GXF_samples_per_frame[] = { 32768, 0 };

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
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;
}

Baptiste Coudurier's avatar
Baptiste Coudurier committed
643 644
static int gxf_write_header(AVFormatContext *s)
{
645
    AVIOContext *pb = s->pb;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
646
    GXFContext *gxf = s->priv_data;
647
    GXFStreamContext *vsc = NULL;
648 649
    uint8_t tracks[255] = {0};
    int i, media_info = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
650

651
    if (!pb->seekable) {
652 653 654 655
        av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome");
        return -1;
    }

Baptiste Coudurier's avatar
Baptiste Coudurier committed
656 657 658
    gxf->flags |= 0x00080000; /* material is simple clip */
    for (i = 0; i < s->nb_streams; ++i) {
        AVStream *st = s->streams[i];
659 660 661 662
        GXFStreamContext *sc = av_mallocz(sizeof(*sc));
        if (!sc)
            return AVERROR(ENOMEM);
        st->priv_data = sc;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
663

664
        sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
665
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679
            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;
680
            avpriv_set_pts_info(st, 64, 1, sc->sample_rate);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
681 682 683 684 685 686
            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 */
687
            media_info = 'A';
688
        } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
689 690 691 692
            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
693
            /* FIXME check from time_base ? */
694
            if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
695 696 697
                sc->frame_rate_index = 5;
                sc->sample_rate = 60;
                gxf->flags |= 0x00000080;
698
                gxf->time_base = (AVRational){ 1001, 60000 };
699
            } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
700 701 702 703
                sc->frame_rate_index = 6;
                sc->media_type++;
                sc->sample_rate = 50;
                gxf->flags |= 0x00000040;
704
                gxf->time_base = (AVRational){ 1, 50 };
705 706 707 708
            } 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
709
            }
710
            avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
711
            if (gxf_find_lines_index(st) < 0)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
712 713 714
                sc->lines_index = -1;
            sc->sample_size = st->codec->bit_rate;
            sc->fields = 2; /* interlaced */
715 716 717

            vsc = sc;

718
            switch (st->codec->codec_id) {
719 720 721 722 723 724 725 726 727 728
            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
729
            case CODEC_ID_MPEG2VIDEO:
730
                sc->first_gop_closed = -1;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
731 732 733
                sc->track_type = 4;
                gxf->mpeg_tracks++;
                gxf->flags |= 0x00008000;
734
                media_info = 'M';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
735 736
                break;
            case CODEC_ID_DVVIDEO:
737
                if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
738 739 740
                    sc->media_type += 2;
                    sc->track_type = 6;
                    gxf->flags |= 0x00002000;
741
                    media_info = 'E';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
742 743 744
                } else {
                    sc->track_type = 5;
                    gxf->flags |= 0x00001000;
745
                    media_info = 'D';
Baptiste Coudurier's avatar
Baptiste Coudurier committed
746 747 748
                }
                break;
            default:
749
                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
750 751 752
                return -1;
            }
        }
753 754
        /* 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]++);
755
        sc->order = s->nb_streams - st->index;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
756
    }
757 758 759 760

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

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

764
    gxf_write_map_packet(s, 0);
765
    gxf_write_flt_packet(s);
766
    gxf_write_umf_packet(s);
767 768 769

    gxf->packet_count = 3;

770
    avio_flush(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
771 772 773
    return 0;
}

774
static int gxf_write_eos_packet(AVIOContext *pb)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
775
{
776
    int64_t pos = avio_tell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
777 778 779 780 781 782 783

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

static int gxf_write_trailer(AVFormatContext *s)
{
784
    GXFContext *gxf = s->priv_data;
785
    AVIOContext *pb = s->pb;
786
    int64_t end;
787
    int i;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
788

789
    ff_audio_interleave_close(s);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
790

791
    gxf_write_eos_packet(pb);