gxfenc.c 24.7 KB
Newer Older
Baptiste Coudurier's avatar
Baptiste Coudurier committed
1 2 3 4
/*
 * GXF muxer.
 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or modify
Baptiste Coudurier's avatar
Baptiste Coudurier committed
8 9 10 11
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * 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 23 24
 */

#include "avformat.h"
#include "gxf.h"
#include "riff.h"
25
#include "fifo.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
26 27 28 29 30

#define GXF_AUDIO_PACKET_SIZE 65536

typedef struct GXFStreamContext {
    AVCodecContext *codec;
31
    AVFifoBuffer audio_buffer;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
32 33 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;
    uint8_t index;
    int frame_rate_index;
    int lines_index;
    int fields;
    int iframes;
    int pframes;
    int bframes;
    int p_per_gop;
    int b_per_gop;
46
    int first_gop_closed;
47
    int64_t current_dts;
48
    int dts_delay;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
} GXFStreamContext;

typedef struct GXFContext {
    uint32_t nb_frames;
    uint32_t material_flags;
    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_user_data_offset;
    uint32_t umf_user_data_size;
    uint32_t umf_length;
    uint16_t umf_track_size;
    uint16_t umf_media_size;
    int audio_written;
    int sample_rate;
    int flags;
    AVFormatContext *fc;
    GXFStreamContext streams[48];
} GXFContext;

typedef struct GXF_Lines {
    int height;
    int index;
} GXF_Lines;


/* FIXME check if it is relevant */
static const GXF_Lines gxf_lines_tab[] = {
    { 480,  1 }, /* NTSC */
    { 512,  1 }, /* NTSC + VBI */
    { 576,  2 }, /* PAL */
    { 608,  2 }, /* PAL + VBI */
    { 1080, 4 },
    { 720,  6 },
};

88
static const AVCodecTag gxf_media_types[] = {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    { 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 */
    { 0, 0 },
};

#define SERVER_PATH "/space/"
#define ES_NAME_PATTERN "ES."

static int gxf_find_lines_index(GXFStreamContext *ctx)
{
    int i;

    for (i = 0; i < 6; ++i) {
        if (ctx->codec->height == gxf_lines_tab[i].height) {
            ctx->lines_index = gxf_lines_tab[i].index;
            return 0;
        }
    }
    return -1;
}

static void gxf_write_padding(ByteIOContext *pb, offset_t to_pad)
{
125
    for (; to_pad > 0; to_pad--) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        put_byte(pb, 0);
    }
}

static offset_t updatePacketSize(ByteIOContext *pb, offset_t pos)
{
    offset_t curpos;
    int size;

    size = url_ftell(pb) - pos;
    if (size % 4) {
        gxf_write_padding(pb, 4 - size % 4);
        size = url_ftell(pb) - pos;
    }
    curpos = url_ftell(pb);
    url_fseek(pb, pos + 6, SEEK_SET);
    put_be32(pb, size);
    url_fseek(pb, curpos, SEEK_SET);
    return curpos - pos;
}

static offset_t updateSize(ByteIOContext *pb, offset_t pos)
{
    offset_t curpos;

    curpos = url_ftell(pb);
    url_fseek(pb, pos, SEEK_SET);
    put_be16(pb, curpos - pos - 2);
    url_fseek(pb, curpos, SEEK_SET);
    return curpos - pos;
}

static void gxf_write_packet_header(ByteIOContext *pb, pkt_type_t type)
{
    put_be32(pb, 0); /* packet leader for synchro */
    put_byte(pb, 1);
    put_byte(pb, type); /* map packet */
    put_be32(pb, 0); /* size */
    put_be32(pb, 0); /* reserved */
    put_byte(pb, 0xE1); /* trailer 1 */
    put_byte(pb, 0xE2); /* trailer 2 */
}

static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
{
    char buffer[1024];
    int size;

    if (ctx->iframes) {
        ctx->p_per_gop = ctx->pframes / ctx->iframes;
        if (ctx->pframes % ctx->iframes)
            ctx->p_per_gop++;
        if (ctx->pframes)
            ctx->b_per_gop = ctx->bframes / ctx->pframes;
        if (ctx->p_per_gop > 9)
            ctx->p_per_gop = 9; /* ensure value won't take more than one char */
        if (ctx->b_per_gop > 9)
            ctx->b_per_gop = 9; /* ensure value won't take more than one char */
    }
    size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
186
                    "Pix 0\nCf %d\nCg %d\nSl 7\nnl16 %d\nVi 1\nf1 1\n",
Baptiste Coudurier's avatar
Baptiste Coudurier committed
187
                    (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop,
188
                    ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, ctx->first_gop_closed == 1,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
                    ctx->codec->height / 16);
    put_byte(pb, 0x4F);
    put_byte(pb, size + 1);
    put_buffer(pb, (uint8_t *)buffer, size + 1);
    return size + 3;
}

static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
{
    /* FIXME implement that */
    put_byte(pb, 0); /* fields */
    put_byte(pb, 0);  /* seconds */
    put_byte(pb, 0); /* minutes */
    put_byte(pb, 0); /* flags + hours */
    /* reserved */
    put_be32(pb, 0);
    return 8;
}

static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stream)
{
    offset_t pos;

    /* track description section */
    put_byte(pb, stream->media_type + 0x80);
    put_byte(pb, stream->index + 0xC0);

    pos = url_ftell(pb);
    put_be16(pb, 0); /* size */

    /* media file name */
    put_byte(pb, 0x4C);
    put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
    put_tag(pb, ES_NAME_PATTERN);
    put_be16(pb, stream->media_info);
    put_byte(pb, 0);

    if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
        /* auxiliary information */
        put_byte(pb, 0x4D);
        put_byte(pb, 8);
        if (stream->codec->codec_id == CODEC_ID_NONE)
            gxf_write_timecode_auxiliary(pb, stream);
        else
            put_le64(pb, 0);
    }

    /* file system version */
    put_byte(pb, 0x4E);
    put_byte(pb, 4);
    put_be32(pb, 0);

    if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
        gxf_write_mpeg_auxiliary(pb, stream);

    /* frame rate */
    put_byte(pb, 0x50);
    put_byte(pb, 4);
    put_be32(pb, stream->frame_rate_index);

    /* lines per frame */
    put_byte(pb, 0x51);
    put_byte(pb, 4);
    put_be32(pb, stream->lines_index);

    /* fields per frame */
    put_byte(pb, 0x52);
    put_byte(pb, 4);
    put_be32(pb, stream->fields);

    return updateSize(pb, pos);
}

static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos;
    const char *filename = strrchr(ctx->fc->filename, '/');

    pos = url_ftell(pb);
    put_be16(pb, 0); /* size */

    /* name */
    if (filename)
        filename++;
    else
        filename = ctx->fc->filename;
    put_byte(pb, 0x40);
    put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
    put_tag(pb, SERVER_PATH);
    put_tag(pb, filename);
    put_byte(pb, 0);

    /* first field */
    put_byte(pb, 0x41);
    put_byte(pb, 4);
    put_be32(pb, 0);

    /* last field */
    put_byte(pb, 0x42);
    put_byte(pb, 4);
    put_be32(pb, ctx->nb_frames);

    /* reserved */
    put_byte(pb, 0x43);
    put_byte(pb, 4);
    put_be32(pb, 0);

    put_byte(pb, 0x44);
    put_byte(pb, 4);
    put_be32(pb, ctx->nb_frames);

    /* estimated size */
    put_byte(pb, 0x45);
    put_byte(pb, 4);
    put_be32(pb, url_fsize(pb) / 1024);

    return updateSize(pb, pos);
}

static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos;
    int i;

    pos = url_ftell(pb);
    put_be16(pb, 0); /* size */
    for (i = 0; i < ctx->fc->nb_streams; ++i)
        gxf_write_track_description(pb, &ctx->streams[i]);
    return updateSize(pb, pos);
}

static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos = url_ftell(pb);

    gxf_write_packet_header(pb, PKT_MAP);

    /* preamble */
    put_byte(pb, 0xE0); /* version */
    put_byte(pb, 0xFF); /* reserved */

    gxf_write_material_data_section(pb, ctx);
    gxf_write_track_description_section(pb, ctx);

    return updatePacketSize(pb, pos);
}

#if 0
static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos = url_ftell(pb);
    int i;

    gxf_write_packet_header(pb, PKT_FLT);

    put_le32(pb, 1000); /* number of fields */
    put_le32(pb, 0); /* number of active flt entries */

    for (i = 0; i < 1000; ++i) {
        put_le32(pb, 0);
    }
    return updatePacketSize(pb, pos);
}
#endif

static int gxf_write_umf_material_description(ByteIOContext *pb, GXFContext *ctx)
{
    put_le32(pb, ctx->flags);
    put_le32(pb, ctx->nb_frames); /* length of the longest track */
    put_le32(pb, ctx->nb_frames); /* length of the shortest track */
    put_le32(pb, 0); /* mark in */
    put_le32(pb, ctx->nb_frames); /* mark out */
    put_le32(pb, 0); /* timecode mark in */
    put_le32(pb, ctx->nb_frames); /* timecode mark out */
    put_le64(pb, ctx->fc->timestamp); /* modification time */
    put_le64(pb, ctx->fc->timestamp); /* creation time */
    put_le16(pb, 0); /* reserved */
    put_le16(pb, 0); /* reserved */
    put_le16(pb, ctx->audio_tracks);
    put_le16(pb, 0); /* timecode track count */
    put_le16(pb, 0); /* reserved */
    put_le16(pb, ctx->mpeg_tracks);
    return 48;
}

static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx)
{
    put_le32(pb, ctx->umf_length); /* total length of the umf data */
    put_le32(pb, 3); /* version */
    put_le32(pb, ctx->fc->nb_streams);
    put_le32(pb, ctx->umf_track_offset); /* umf track section offset */
    put_le32(pb, ctx->umf_track_size);
    put_le32(pb, ctx->fc->nb_streams);
    put_le32(pb, ctx->umf_media_offset);
    put_le32(pb, ctx->umf_media_size);
    put_le32(pb, ctx->umf_user_data_offset); /* user data offset */
    put_le32(pb, ctx->umf_user_data_size); /* user data size */
    put_le32(pb, 0); /* reserved */
    put_le32(pb, 0); /* reserved */
    return 48;
}

static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos = url_ftell(pb);
    int tracks[255]={0};
    int i;

    ctx->umf_track_offset = pos - ctx->umf_start_offset;
    for (i = 0; i < ctx->fc->nb_streams; ++i) {
        AVStream *st = ctx->fc->streams[i];
        GXFStreamContext *sc = &ctx->streams[i];
        int id = 0;

        switch (st->codec->codec_id) {
        case CODEC_ID_MPEG1VIDEO: id= 'L'; break;
        case CODEC_ID_MPEG2VIDEO: id= 'M'; break;
        case CODEC_ID_PCM_S16LE:  id= 'A'; break;
        case CODEC_ID_DVVIDEO:    id= sc->track_type == 6 ? 'E' : 'D'; break;
        case CODEC_ID_MJPEG:      id= 'V'; break;
409
        default:                  break;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        }
        sc->media_info= id << 8;
        /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
        sc->media_info |= '0' + (tracks[id]++);
        put_le16(pb, sc->media_info);
        put_le16(pb, 1);
    }
    return url_ftell(pb) - pos;
}

static int gxf_write_umf_media_mpeg(ByteIOContext *pb, GXFStreamContext *stream)
{
    if (stream->codec->pix_fmt == PIX_FMT_YUV422P)
        put_le32(pb, 2);
    else
        put_le32(pb, 1); /* default to 420 */
426
    put_le32(pb, stream->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
    put_le32(pb, 1); /* I picture per GOP */
    put_le32(pb, stream->p_per_gop);
    put_le32(pb, stream->b_per_gop);
    if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
        put_le32(pb, 2);
    else if (stream->codec->codec_id == CODEC_ID_MPEG1VIDEO)
        put_le32(pb, 1);
    else
        put_le32(pb, 0);
    put_le32(pb, 0); /* reserved */
    return 32;
}

static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *track)
{
    /* FIXME implement */
    put_be32(pb, 0); /* drop frame flag */
    put_be32(pb, 0); /* reserved */
    put_be32(pb, 0); /* reserved */
    put_be32(pb, 0); /* reserved */
    put_be32(pb, 0); /* reserved */
    put_be32(pb, 0); /* reserved */
    put_be32(pb, 0); /* reserved */
    put_be32(pb, 0); /* reserved */
    return 32;
}

static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *track)
{
    int i;

    for (i = 0; i < 8; i++) {
        put_be32(pb, 0);
    }
    return 32;
}

static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *track)
{
    put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
    put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
    put_le32(pb, 0); /* number of fields over which to ramp up sound level */
    put_le32(pb, 0); /* number of fields over which to ramp down sound level */
    put_le32(pb, 0); /* reserved */
    put_le32(pb, 0); /* reserved */
    return 32;
}

#if 0
static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track)
{
    put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
    put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
    put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
    put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
    return 32;
}
#endif

static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos;
    int i;

    pos = url_ftell(pb);
    ctx->umf_media_offset = pos - ctx->umf_start_offset;
    for (i = 0; i < ctx->fc->nb_streams; ++i) {
        GXFStreamContext *sc = &ctx->streams[i];
        char buffer[88];
        offset_t startpos, curpos;
        int path_size = strlen(ES_NAME_PATTERN);

Baptiste Coudurier's avatar
Baptiste Coudurier committed
500
        memset(buffer, 0, 88);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
        startpos = url_ftell(pb);
        put_le16(pb, 0); /* length */
        put_le16(pb, sc->media_info);
        put_le16(pb, 0); /* reserved */
        put_le16(pb, 0); /* reserved */
        put_le32(pb, ctx->nb_frames);
        put_le32(pb, 0); /* attributes rw, ro */
        put_le32(pb, 0); /* mark in */
        put_le32(pb, ctx->nb_frames); /* mark out */
        strncpy(buffer, ES_NAME_PATTERN, path_size);
        put_buffer(pb, (uint8_t *)buffer, path_size);
        put_be16(pb, sc->media_info);
        put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
        put_le32(pb, sc->track_type);
        put_le32(pb, sc->sample_rate);
        put_le32(pb, sc->sample_size);
        put_le32(pb, 0); /* reserved */
        switch (sc->codec->codec_id) {
        case CODEC_ID_MPEG2VIDEO:
            gxf_write_umf_media_mpeg(pb, sc);
            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;
        default:
            gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
        }
        curpos = url_ftell(pb);
        url_fseek(pb, startpos, SEEK_SET);
        put_le16(pb, curpos - startpos);
        url_fseek(pb, curpos, SEEK_SET);
    }
    return url_ftell(pb) - pos;
}

static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos = url_ftell(pb);
    ctx->umf_user_data_offset = pos - ctx->umf_start_offset;
    put_le32(pb, 20);
    put_le32(pb,  0);
    put_le16(pb,  0);
    put_le16(pb,  0);
    put_le32(pb,  0);
    put_byte(pb,  0);
    put_byte(pb,  0);
    put_byte(pb,  0);
    put_byte(pb,  0);
    return 20;
}

static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos = url_ftell(pb);

    gxf_write_packet_header(pb, PKT_UMF);

    /* preamble */
    put_byte(pb, 3); /* first and last (only) packet */
    put_be32(pb, ctx->umf_length); /* data length */

    ctx->umf_start_offset = url_ftell(pb);
    gxf_write_umf_payload(pb, ctx);
    gxf_write_umf_material_description(pb, ctx);
    ctx->umf_track_size = gxf_write_umf_track_description(pb, ctx);
    ctx->umf_media_size = gxf_write_umf_media_description(pb, ctx);
    ctx->umf_user_data_size = gxf_write_umf_user_data(pb, ctx);
    ctx->umf_length = url_ftell(pb) - ctx->umf_start_offset;
    return updatePacketSize(pb, pos);
}

Baptiste Coudurier's avatar
Baptiste Coudurier committed
575 576
#define GXF_NODELAY -5000

Baptiste Coudurier's avatar
Baptiste Coudurier committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
static int gxf_write_header(AVFormatContext *s)
{
    ByteIOContext *pb = &s->pb;
    GXFContext *gxf = s->priv_data;
    int i;

    gxf->fc = s;
    gxf->flags |= 0x00080000; /* material is simple clip */
    for (i = 0; i < s->nb_streams; ++i) {
        AVStream *st = s->streams[i];
        GXFStreamContext *sc = &gxf->streams[i];

        sc->codec = st->codec;
        sc->index = i;
        sc->media_type = codec_get_tag(gxf_media_types, sc->codec->codec_id);
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
            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;
607
            av_set_pts_info(st, 64, 1, sc->sample_rate);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
608 609 610 611 612 613
            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 */
614
            av_fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
615 616 617 618 619 620 621 622 623 624 625 626 627
        } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) {
            /* FIXME check from time_base ? */
            if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */
                sc->frame_rate_index = 5;
                sc->sample_rate = 60;
                gxf->flags |= 0x00000080;
            } else { /* assume PAL */
                sc->frame_rate_index = 6;
                sc->media_type++;
                sc->sample_rate = 50;
                gxf->flags |= 0x00000040;
            }
            gxf->sample_rate = sc->sample_rate;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
628 629
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
            sc->dts_delay = GXF_NODELAY;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
630 631 632 633 634 635
            if (gxf_find_lines_index(sc) < 0)
                sc->lines_index = -1;
            sc->sample_size = st->codec->bit_rate;
            sc->fields = 2; /* interlaced */
            switch (sc->codec->codec_id) {
            case CODEC_ID_MPEG2VIDEO:
636
                sc->first_gop_closed = -1;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
                sc->track_type = 4;
                gxf->mpeg_tracks++;
                gxf->flags |= 0x00008000;
                break;
            case CODEC_ID_DVVIDEO:
                if (sc->codec->pix_fmt == PIX_FMT_YUV422P) {
                    sc->media_type += 2;
                    sc->track_type = 6;
                    gxf->flags |= 0x00002000;
                } else {
                    sc->track_type = 5;
                    gxf->flags |= 0x00001000;
                }
                break;
            default:
652
                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
                return -1;
            }
        }
    }
    gxf_write_map_packet(pb, gxf);
    //gxf_write_flt_packet(pb, gxf);
    gxf_write_umf_packet(pb, gxf);
    put_flush_packet(pb);
    return 0;
}

static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
{
    offset_t pos = url_ftell(pb);

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

static int gxf_write_trailer(AVFormatContext *s)
{
    ByteIOContext *pb = &s->pb;
    GXFContext *gxf = s->priv_data;
    offset_t end;
    int i;

    for (i = 0; i < s->nb_streams; ++i) {
        if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
681
            av_fifo_free(&gxf->streams[i].audio_buffer);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
        }
        if (s->streams[i]->codec->frame_number > gxf->nb_frames)
            gxf->nb_frames = 2 * s->streams[i]->codec->frame_number;
    }

    gxf_write_eos_packet(pb, gxf);
    end = url_ftell(pb);
    url_fseek(pb, 0, SEEK_SET);
    /* overwrite map and umf packets with new values */
    gxf_write_map_packet(pb, gxf);
    //gxf_write_flt_packet(pb, gxf);
    gxf_write_umf_packet(pb, gxf);
    url_fseek(pb, end, SEEK_SET);
    return 0;
}

698 699 700 701 702 703
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];
704
        if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
705
            sc->first_gop_closed= (buf[i+4]>>6)&1;
706 707 708 709
    }
    return (buf[i+1]>>3)&7;
}

Baptiste Coudurier's avatar
Baptiste Coudurier committed
710 711 712
static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size)
{
    GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
Baptiste Coudurier's avatar
Baptiste Coudurier committed
713
    int64_t dts = av_rescale(pkt->dts, ctx->sample_rate, sc->codec->time_base.den);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
714 715 716

    put_byte(pb, sc->media_type);
    put_byte(pb, sc->index);
717
    put_be32(pb, dts);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
718 719 720 721
    if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
        put_be16(pb, 0);
        put_be16(pb, size / 2);
    } else if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
722 723
        int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
        if (frame_type == FF_I_TYPE) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
724 725
            put_byte(pb, 0x0d);
            sc->iframes++;
726
        } else if (frame_type == FF_B_TYPE) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
727 728 729 730 731 732 733 734 735 736 737 738
            put_byte(pb, 0x0f);
            sc->bframes++;
        } else {
            put_byte(pb, 0x0e);
            sc->pframes++;
        }
        put_be24(pb, size);
    } else if (sc->codec->codec_id == CODEC_ID_DVVIDEO) {
        put_byte(pb, size / 4096);
        put_be24(pb, 0);
    } else
        put_be32(pb, size);
739
    put_be32(pb, dts);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753
    put_byte(pb, 1); /* flags */
    put_byte(pb, 0); /* reserved */
    return 16;
}

static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt)
{
    GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
    offset_t pos = url_ftell(pb);
    int padding = 0;

    gxf_write_packet_header(pb, PKT_MEDIA);
    if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
        padding = 4 - pkt->size % 4;
754 755
    else if (sc->codec->codec_type == CODEC_TYPE_AUDIO)
        padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
756
    gxf_write_media_preamble(pb, ctx, pkt, pkt->size + padding);
757
    put_buffer(pb, pkt->data, pkt->size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
758 759 760 761 762 763 764 765 766 767 768 769 770
    gxf_write_padding(pb, padding);
    return updatePacketSize(pb, pos);
}

static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
{
    GXFContext *gxf = s->priv_data;

    gxf_write_media_packet(&s->pb, gxf, pkt);
    put_flush_packet(&s->pb);
    return 0;
}

771 772
static int gxf_new_audio_packet(GXFContext *gxf, GXFStreamContext *sc, AVPacket *pkt, int flush)
{
773
    int size = flush ? av_fifo_size(&sc->audio_buffer) : GXF_AUDIO_PACKET_SIZE;
774 775 776 777

    if (!size)
        return 0;
    av_new_packet(pkt, size);
778
    av_fifo_read(&sc->audio_buffer, pkt->data, size);
779 780 781 782 783 784
    pkt->stream_index = sc->index;
    pkt->dts = sc->current_dts;
    sc->current_dts += size / 2; /* we only support 16 bit pcm mono for now */
    return size;
}

Baptiste Coudurier's avatar
Baptiste Coudurier committed
785 786 787
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
{
    GXFContext *gxf = s->priv_data;
788
    AVPacket new_pkt;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
789 790
    int i;

791
    for (i = 0; i < s->nb_streams; i++) {
792 793 794
        AVStream *st = s->streams[i];
        GXFStreamContext *sc = &gxf->streams[i];
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
795
            if (pkt && pkt->stream_index == i) {
796
                av_fifo_write(&sc->audio_buffer, pkt->data, pkt->size);
797
                pkt = NULL;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
798
            }
799
            if (flush || av_fifo_size(&sc->audio_buffer) >= GXF_AUDIO_PACKET_SIZE) {
800
                if (!pkt && gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) {
801 802 803
                    pkt = &new_pkt;
                    break; /* add pkt right now into list */
                }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
804
            }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
805 806 807
        } else if (pkt && pkt->stream_index == i) {
            if (sc->dts_delay == GXF_NODELAY) /* adjust dts if needed */
                sc->dts_delay = pkt->dts;
808
            pkt->dts -= sc->dts_delay;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
809 810
        }
    }
811
    return av_interleave_packet_per_dts(s, out, pkt, flush);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
}

AVOutputFormat gxf_muxer = {
    "gxf",
    "GXF format",
    NULL,
    "gxf",
    sizeof(GXFContext),
    CODEC_ID_PCM_S16LE,
    CODEC_ID_MPEG2VIDEO,
    gxf_write_header,
    gxf_write_packet,
    gxf_write_trailer,
    0,
    NULL,
    gxf_interleave_packet,
};