Statistics
| Branch: | Revision:

ffmpeg / libavformat / gxfenc.c @ c60208e7

History | View | Annotate | Download (25.4 KB)

1 86329cce Baptiste Coudurier
/*
2
 * GXF muxer.
3 406792e7 Diego Biurrun
 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4 86329cce Baptiste Coudurier
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7 71daa04c Baptiste Coudurier
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11 86329cce Baptiste Coudurier
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 86329cce Baptiste Coudurier
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 71daa04c Baptiste Coudurier
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 86329cce Baptiste Coudurier
 *
17 71daa04c Baptiste Coudurier
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19 b78e7197 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 86329cce Baptiste Coudurier
 */
21
22 245976da Diego Biurrun
#include "libavutil/fifo.h"
23 86329cce Baptiste Coudurier
#include "avformat.h"
24
#include "gxf.h"
25
#include "riff.h"
26
27
#define GXF_AUDIO_PACKET_SIZE 65536
28
29
typedef struct GXFStreamContext {
30
    AVCodecContext *codec;
31 f5a478f6 Roman Shaposhnik
    AVFifoBuffer audio_buffer;
32 86329cce Baptiste Coudurier
    uint32_t track_type;
33
    uint32_t sample_size;
34
    uint32_t sample_rate;
35
    uint16_t media_type;
36
    uint16_t media_info;
37
    uint8_t index;
38
    int frame_rate_index;
39
    int lines_index;
40
    int fields;
41
    int iframes;
42
    int pframes;
43
    int bframes;
44
    int p_per_gop;
45
    int b_per_gop;
46 2b7e230b Baptiste Coudurier
    int first_gop_closed;
47 201f1d45 Baptiste Coudurier
    int64_t current_dts;
48 6a287fd7 Baptiste Coudurier
    int dts_delay;
49 86329cce Baptiste Coudurier
} GXFStreamContext;
50
51
typedef struct GXFContext {
52
    uint32_t nb_frames;
53
    uint32_t material_flags;
54
    uint16_t audio_tracks;
55
    uint16_t mpeg_tracks;
56
    int64_t creation_time;
57
    uint32_t umf_start_offset;
58
    uint32_t umf_track_offset;
59
    uint32_t umf_media_offset;
60
    uint32_t umf_user_data_offset;
61
    uint32_t umf_user_data_size;
62
    uint32_t umf_length;
63
    uint16_t umf_track_size;
64
    uint16_t umf_media_size;
65
    int audio_written;
66
    int sample_rate;
67
    int flags;
68
    AVFormatContext *fc;
69
    GXFStreamContext streams[48];
70
} GXFContext;
71
72
typedef struct GXF_Lines {
73
    int height;
74
    int index;
75
} GXF_Lines;
76
77
78
/* FIXME check if it is relevant */
79
static const GXF_Lines gxf_lines_tab[] = {
80
    { 480,  1 }, /* NTSC */
81
    { 512,  1 }, /* NTSC + VBI */
82
    { 576,  2 }, /* PAL */
83
    { 608,  2 }, /* PAL + VBI */
84
    { 1080, 4 },
85
    { 720,  6 },
86
};
87
88 7caf0cc6 Michael Niedermayer
static const AVCodecTag gxf_media_types[] = {
89 86329cce Baptiste Coudurier
    { CODEC_ID_MJPEG     ,   3 }, /* NTSC */
90
    { CODEC_ID_MJPEG     ,   4 }, /* PAL */
91
    { CODEC_ID_PCM_S24LE ,   9 },
92
    { CODEC_ID_PCM_S16LE ,  10 },
93
    { CODEC_ID_MPEG2VIDEO,  11 }, /* NTSC */
94
    { CODEC_ID_MPEG2VIDEO,  12 }, /* PAL */
95
    { CODEC_ID_DVVIDEO   ,  13 }, /* NTSC */
96
    { CODEC_ID_DVVIDEO   ,  14 }, /* PAL */
97
    { CODEC_ID_DVVIDEO   ,  15 }, /* 50M NTSC */
98
    { CODEC_ID_DVVIDEO   ,  16 }, /* 50M PAL */
99
    { CODEC_ID_AC3       ,  17 },
100
    //{ CODEC_ID_NONE,  ,   18 }, /* Non compressed 24 bit audio */
101
    { CODEC_ID_MPEG2VIDEO,  20 }, /* MPEG HD */
102
    { CODEC_ID_MPEG1VIDEO,  22 }, /* NTSC */
103
    { CODEC_ID_MPEG1VIDEO,  23 }, /* PAL */
104
    { 0, 0 },
105
};
106
107
#define SERVER_PATH "/space/"
108
#define ES_NAME_PATTERN "ES."
109
110
static int gxf_find_lines_index(GXFStreamContext *ctx)
111
{
112
    int i;
113
114
    for (i = 0; i < 6; ++i) {
115
        if (ctx->codec->height == gxf_lines_tab[i].height) {
116
            ctx->lines_index = gxf_lines_tab[i].index;
117
            return 0;
118
        }
119
    }
120
    return -1;
121
}
122
123 bc5c918e Diego Biurrun
static void gxf_write_padding(ByteIOContext *pb, int64_t to_pad)
124 86329cce Baptiste Coudurier
{
125 0e15b7b0 Baptiste Coudurier
    for (; to_pad > 0; to_pad--) {
126 86329cce Baptiste Coudurier
        put_byte(pb, 0);
127
    }
128
}
129
130 bc5c918e Diego Biurrun
static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos)
131 86329cce Baptiste Coudurier
{
132 bc5c918e Diego Biurrun
    int64_t curpos;
133 86329cce Baptiste Coudurier
    int size;
134
135
    size = url_ftell(pb) - pos;
136
    if (size % 4) {
137
        gxf_write_padding(pb, 4 - size % 4);
138
        size = url_ftell(pb) - pos;
139
    }
140
    curpos = url_ftell(pb);
141
    url_fseek(pb, pos + 6, SEEK_SET);
142
    put_be32(pb, size);
143
    url_fseek(pb, curpos, SEEK_SET);
144
    return curpos - pos;
145
}
146
147 bc5c918e Diego Biurrun
static int64_t updateSize(ByteIOContext *pb, int64_t pos)
148 86329cce Baptiste Coudurier
{
149 bc5c918e Diego Biurrun
    int64_t curpos;
150 86329cce Baptiste Coudurier
151
    curpos = url_ftell(pb);
152
    url_fseek(pb, pos, SEEK_SET);
153
    put_be16(pb, curpos - pos - 2);
154
    url_fseek(pb, curpos, SEEK_SET);
155
    return curpos - pos;
156
}
157
158 d77ac324 Reimar Döffinger
static void gxf_write_packet_header(ByteIOContext *pb, GXFPktType type)
159 86329cce Baptiste Coudurier
{
160
    put_be32(pb, 0); /* packet leader for synchro */
161
    put_byte(pb, 1);
162
    put_byte(pb, type); /* map packet */
163
    put_be32(pb, 0); /* size */
164
    put_be32(pb, 0); /* reserved */
165
    put_byte(pb, 0xE1); /* trailer 1 */
166
    put_byte(pb, 0xE2); /* trailer 2 */
167
}
168
169
static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
170
{
171
    char buffer[1024];
172 15a4e8b8 Baptiste Coudurier
    int size, starting_line;
173 86329cce Baptiste Coudurier
174
    if (ctx->iframes) {
175
        ctx->p_per_gop = ctx->pframes / ctx->iframes;
176
        if (ctx->pframes % ctx->iframes)
177
            ctx->p_per_gop++;
178
        if (ctx->pframes)
179
            ctx->b_per_gop = ctx->bframes / ctx->pframes;
180
        if (ctx->p_per_gop > 9)
181
            ctx->p_per_gop = 9; /* ensure value won't take more than one char */
182
        if (ctx->b_per_gop > 9)
183
            ctx->b_per_gop = 9; /* ensure value won't take more than one char */
184
    }
185 15a4e8b8 Baptiste Coudurier
    if (ctx->codec->height == 512 || ctx->codec->height == 608)
186
        starting_line = 7; // VBI
187
    else if (ctx->codec->height == 480)
188
        starting_line = 20;
189
    else
190
        starting_line = 23; // default PAL
191
192 86329cce Baptiste Coudurier
    size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
193 15a4e8b8 Baptiste Coudurier
                    "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
194 86329cce Baptiste Coudurier
                    (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop,
195 afbbe5e3 Baptiste Coudurier
                    ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, ctx->first_gop_closed == 1,
196 15a4e8b8 Baptiste Coudurier
                    starting_line, ctx->codec->height / 16);
197 b1dc5beb Aurelien Jacobs
    put_byte(pb, TRACK_MPG_AUX);
198 86329cce Baptiste Coudurier
    put_byte(pb, size + 1);
199
    put_buffer(pb, (uint8_t *)buffer, size + 1);
200
    return size + 3;
201
}
202
203
static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
204
{
205
    /* FIXME implement that */
206
    put_byte(pb, 0); /* fields */
207
    put_byte(pb, 0);  /* seconds */
208
    put_byte(pb, 0); /* minutes */
209
    put_byte(pb, 0); /* flags + hours */
210
    /* reserved */
211
    put_be32(pb, 0);
212
    return 8;
213
}
214
215
static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stream)
216
{
217 bc5c918e Diego Biurrun
    int64_t pos;
218 86329cce Baptiste Coudurier
219
    /* track description section */
220
    put_byte(pb, stream->media_type + 0x80);
221
    put_byte(pb, stream->index + 0xC0);
222
223
    pos = url_ftell(pb);
224
    put_be16(pb, 0); /* size */
225
226
    /* media file name */
227 b1dc5beb Aurelien Jacobs
    put_byte(pb, TRACK_NAME);
228 86329cce Baptiste Coudurier
    put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
229
    put_tag(pb, ES_NAME_PATTERN);
230
    put_be16(pb, stream->media_info);
231
    put_byte(pb, 0);
232
233
    if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
234
        /* auxiliary information */
235 b1dc5beb Aurelien Jacobs
        put_byte(pb, TRACK_AUX);
236 86329cce Baptiste Coudurier
        put_byte(pb, 8);
237
        if (stream->codec->codec_id == CODEC_ID_NONE)
238
            gxf_write_timecode_auxiliary(pb, stream);
239
        else
240
            put_le64(pb, 0);
241
    }
242
243
    /* file system version */
244 b1dc5beb Aurelien Jacobs
    put_byte(pb, TRACK_VER);
245 86329cce Baptiste Coudurier
    put_byte(pb, 4);
246
    put_be32(pb, 0);
247
248
    if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
249
        gxf_write_mpeg_auxiliary(pb, stream);
250
251
    /* frame rate */
252 b1dc5beb Aurelien Jacobs
    put_byte(pb, TRACK_FPS);
253 86329cce Baptiste Coudurier
    put_byte(pb, 4);
254
    put_be32(pb, stream->frame_rate_index);
255
256
    /* lines per frame */
257 b1dc5beb Aurelien Jacobs
    put_byte(pb, TRACK_LINES);
258 86329cce Baptiste Coudurier
    put_byte(pb, 4);
259
    put_be32(pb, stream->lines_index);
260
261
    /* fields per frame */
262 b1dc5beb Aurelien Jacobs
    put_byte(pb, TRACK_FPF);
263 86329cce Baptiste Coudurier
    put_byte(pb, 4);
264
    put_be32(pb, stream->fields);
265
266
    return updateSize(pb, pos);
267
}
268
269
static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
270
{
271 bc5c918e Diego Biurrun
    int64_t pos;
272 86329cce Baptiste Coudurier
    const char *filename = strrchr(ctx->fc->filename, '/');
273
274
    pos = url_ftell(pb);
275
    put_be16(pb, 0); /* size */
276
277
    /* name */
278
    if (filename)
279
        filename++;
280
    else
281
        filename = ctx->fc->filename;
282 b1dc5beb Aurelien Jacobs
    put_byte(pb, MAT_NAME);
283 86329cce Baptiste Coudurier
    put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
284
    put_tag(pb, SERVER_PATH);
285
    put_tag(pb, filename);
286
    put_byte(pb, 0);
287
288
    /* first field */
289 b1dc5beb Aurelien Jacobs
    put_byte(pb, MAT_FIRST_FIELD);
290 86329cce Baptiste Coudurier
    put_byte(pb, 4);
291
    put_be32(pb, 0);
292
293
    /* last field */
294 b1dc5beb Aurelien Jacobs
    put_byte(pb, MAT_LAST_FIELD);
295 86329cce Baptiste Coudurier
    put_byte(pb, 4);
296
    put_be32(pb, ctx->nb_frames);
297
298
    /* reserved */
299 b1dc5beb Aurelien Jacobs
    put_byte(pb, MAT_MARK_IN);
300 86329cce Baptiste Coudurier
    put_byte(pb, 4);
301
    put_be32(pb, 0);
302
303 b1dc5beb Aurelien Jacobs
    put_byte(pb, MAT_MARK_OUT);
304 86329cce Baptiste Coudurier
    put_byte(pb, 4);
305
    put_be32(pb, ctx->nb_frames);
306
307
    /* estimated size */
308 b1dc5beb Aurelien Jacobs
    put_byte(pb, MAT_SIZE);
309 86329cce Baptiste Coudurier
    put_byte(pb, 4);
310
    put_be32(pb, url_fsize(pb) / 1024);
311
312
    return updateSize(pb, pos);
313
}
314
315
static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx)
316
{
317 bc5c918e Diego Biurrun
    int64_t pos;
318 86329cce Baptiste Coudurier
    int i;
319
320
    pos = url_ftell(pb);
321
    put_be16(pb, 0); /* size */
322
    for (i = 0; i < ctx->fc->nb_streams; ++i)
323
        gxf_write_track_description(pb, &ctx->streams[i]);
324
    return updateSize(pb, pos);
325
}
326
327
static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
328
{
329 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
330 86329cce Baptiste Coudurier
331
    gxf_write_packet_header(pb, PKT_MAP);
332
333
    /* preamble */
334
    put_byte(pb, 0xE0); /* version */
335
    put_byte(pb, 0xFF); /* reserved */
336
337
    gxf_write_material_data_section(pb, ctx);
338
    gxf_write_track_description_section(pb, ctx);
339
340
    return updatePacketSize(pb, pos);
341
}
342
343
#if 0
344
static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx)
345
{
346 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
347 86329cce Baptiste Coudurier
    int i;
348

349
    gxf_write_packet_header(pb, PKT_FLT);
350

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

354
    for (i = 0; i < 1000; ++i) {
355
        put_le32(pb, 0);
356
    }
357
    return updatePacketSize(pb, pos);
358
}
359
#endif
360
361
static int gxf_write_umf_material_description(ByteIOContext *pb, GXFContext *ctx)
362
{
363 fc941878 Baptiste Coudurier
    // XXX drop frame
364
    uint32_t timecode =
365 e9338da8 Baptiste Coudurier
        ctx->nb_frames / ctx->sample_rate * 3600 % 24 << 27 | // hours
366
        ctx->nb_frames / ctx->sample_rate * 60 % 60   << 16 | // minutes
367
        ctx->nb_frames / ctx->sample_rate % 60        <<  8 | // seconds
368
        ctx->nb_frames % ctx->sample_rate;                    // fields
369 fc941878 Baptiste Coudurier
370 86329cce Baptiste Coudurier
    put_le32(pb, ctx->flags);
371
    put_le32(pb, ctx->nb_frames); /* length of the longest track */
372
    put_le32(pb, ctx->nb_frames); /* length of the shortest track */
373
    put_le32(pb, 0); /* mark in */
374
    put_le32(pb, ctx->nb_frames); /* mark out */
375
    put_le32(pb, 0); /* timecode mark in */
376 fc941878 Baptiste Coudurier
    put_le32(pb, timecode); /* timecode mark out */
377 86329cce Baptiste Coudurier
    put_le64(pb, ctx->fc->timestamp); /* modification time */
378
    put_le64(pb, ctx->fc->timestamp); /* creation time */
379
    put_le16(pb, 0); /* reserved */
380
    put_le16(pb, 0); /* reserved */
381
    put_le16(pb, ctx->audio_tracks);
382
    put_le16(pb, 0); /* timecode track count */
383
    put_le16(pb, 0); /* reserved */
384
    put_le16(pb, ctx->mpeg_tracks);
385
    return 48;
386
}
387
388
static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx)
389
{
390
    put_le32(pb, ctx->umf_length); /* total length of the umf data */
391
    put_le32(pb, 3); /* version */
392
    put_le32(pb, ctx->fc->nb_streams);
393
    put_le32(pb, ctx->umf_track_offset); /* umf track section offset */
394
    put_le32(pb, ctx->umf_track_size);
395
    put_le32(pb, ctx->fc->nb_streams);
396
    put_le32(pb, ctx->umf_media_offset);
397
    put_le32(pb, ctx->umf_media_size);
398
    put_le32(pb, ctx->umf_user_data_offset); /* user data offset */
399
    put_le32(pb, ctx->umf_user_data_size); /* user data size */
400
    put_le32(pb, 0); /* reserved */
401
    put_le32(pb, 0); /* reserved */
402
    return 48;
403
}
404
405
static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
406
{
407 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
408 86329cce Baptiste Coudurier
    int tracks[255]={0};
409
    int i;
410
411
    ctx->umf_track_offset = pos - ctx->umf_start_offset;
412
    for (i = 0; i < ctx->fc->nb_streams; ++i) {
413
        AVStream *st = ctx->fc->streams[i];
414
        GXFStreamContext *sc = &ctx->streams[i];
415
        int id = 0;
416
417
        switch (st->codec->codec_id) {
418
        case CODEC_ID_MPEG1VIDEO: id= 'L'; break;
419
        case CODEC_ID_MPEG2VIDEO: id= 'M'; break;
420
        case CODEC_ID_PCM_S16LE:  id= 'A'; break;
421
        case CODEC_ID_DVVIDEO:    id= sc->track_type == 6 ? 'E' : 'D'; break;
422
        case CODEC_ID_MJPEG:      id= 'V'; break;
423 31903634 Baptiste Coudurier
        default:                  break;
424 86329cce Baptiste Coudurier
        }
425
        sc->media_info= id << 8;
426
        /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
427
        sc->media_info |= '0' + (tracks[id]++);
428
        put_le16(pb, sc->media_info);
429
        put_le16(pb, 1);
430
    }
431
    return url_ftell(pb) - pos;
432
}
433
434
static int gxf_write_umf_media_mpeg(ByteIOContext *pb, GXFStreamContext *stream)
435
{
436
    if (stream->codec->pix_fmt == PIX_FMT_YUV422P)
437
        put_le32(pb, 2);
438
    else
439
        put_le32(pb, 1); /* default to 420 */
440 afbbe5e3 Baptiste Coudurier
    put_le32(pb, stream->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
441 86329cce Baptiste Coudurier
    put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
442
    put_le32(pb, 1); /* I picture per GOP */
443
    put_le32(pb, stream->p_per_gop);
444
    put_le32(pb, stream->b_per_gop);
445
    if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
446
        put_le32(pb, 2);
447
    else if (stream->codec->codec_id == CODEC_ID_MPEG1VIDEO)
448
        put_le32(pb, 1);
449
    else
450
        put_le32(pb, 0);
451
    put_le32(pb, 0); /* reserved */
452
    return 32;
453
}
454
455
static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *track)
456
{
457
    /* FIXME implement */
458
    put_be32(pb, 0); /* drop frame flag */
459
    put_be32(pb, 0); /* reserved */
460
    put_be32(pb, 0); /* reserved */
461
    put_be32(pb, 0); /* reserved */
462
    put_be32(pb, 0); /* reserved */
463
    put_be32(pb, 0); /* reserved */
464
    put_be32(pb, 0); /* reserved */
465
    put_be32(pb, 0); /* reserved */
466
    return 32;
467
}
468
469
static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *track)
470
{
471
    int i;
472
473
    for (i = 0; i < 8; i++) {
474
        put_be32(pb, 0);
475
    }
476
    return 32;
477
}
478
479
static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *track)
480
{
481
    put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
482
    put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
483
    put_le32(pb, 0); /* number of fields over which to ramp up sound level */
484
    put_le32(pb, 0); /* number of fields over which to ramp down sound level */
485
    put_le32(pb, 0); /* reserved */
486
    put_le32(pb, 0); /* reserved */
487
    return 32;
488
}
489
490
#if 0
491
static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track)
492
{
493
    put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
494
    put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
495
    put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
496
    put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
497
    return 32;
498
}
499
#endif
500
501
static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
502
{
503 bc5c918e Diego Biurrun
    int64_t pos;
504 86329cce Baptiste Coudurier
    int i;
505
506
    pos = url_ftell(pb);
507
    ctx->umf_media_offset = pos - ctx->umf_start_offset;
508
    for (i = 0; i < ctx->fc->nb_streams; ++i) {
509
        GXFStreamContext *sc = &ctx->streams[i];
510
        char buffer[88];
511 bc5c918e Diego Biurrun
        int64_t startpos, curpos;
512 86329cce Baptiste Coudurier
        int path_size = strlen(ES_NAME_PATTERN);
513
514 6a7eba1f Baptiste Coudurier
        memset(buffer, 0, 88);
515 86329cce Baptiste Coudurier
        startpos = url_ftell(pb);
516
        put_le16(pb, 0); /* length */
517
        put_le16(pb, sc->media_info);
518
        put_le16(pb, 0); /* reserved */
519
        put_le16(pb, 0); /* reserved */
520
        put_le32(pb, ctx->nb_frames);
521
        put_le32(pb, 0); /* attributes rw, ro */
522
        put_le32(pb, 0); /* mark in */
523
        put_le32(pb, ctx->nb_frames); /* mark out */
524
        strncpy(buffer, ES_NAME_PATTERN, path_size);
525
        put_buffer(pb, (uint8_t *)buffer, path_size);
526
        put_be16(pb, sc->media_info);
527
        put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
528
        put_le32(pb, sc->track_type);
529
        put_le32(pb, sc->sample_rate);
530
        put_le32(pb, sc->sample_size);
531
        put_le32(pb, 0); /* reserved */
532
        switch (sc->codec->codec_id) {
533
        case CODEC_ID_MPEG2VIDEO:
534
            gxf_write_umf_media_mpeg(pb, sc);
535
            break;
536
        case CODEC_ID_PCM_S16LE:
537
            gxf_write_umf_media_audio(pb, sc);
538
            break;
539
        case CODEC_ID_DVVIDEO:
540
            gxf_write_umf_media_dv(pb, sc);
541
            break;
542
        default:
543
            gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
544
        }
545
        curpos = url_ftell(pb);
546
        url_fseek(pb, startpos, SEEK_SET);
547
        put_le16(pb, curpos - startpos);
548
        url_fseek(pb, curpos, SEEK_SET);
549
    }
550
    return url_ftell(pb) - pos;
551
}
552
553
static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
554
{
555 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
556 86329cce Baptiste Coudurier
    ctx->umf_user_data_offset = pos - ctx->umf_start_offset;
557
    put_le32(pb, 20);
558
    put_le32(pb,  0);
559
    put_le16(pb,  0);
560
    put_le16(pb,  0);
561
    put_le32(pb,  0);
562
    put_byte(pb,  0);
563
    put_byte(pb,  0);
564
    put_byte(pb,  0);
565
    put_byte(pb,  0);
566
    return 20;
567
}
568
569
static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
570
{
571 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
572 86329cce Baptiste Coudurier
573
    gxf_write_packet_header(pb, PKT_UMF);
574
575
    /* preamble */
576
    put_byte(pb, 3); /* first and last (only) packet */
577
    put_be32(pb, ctx->umf_length); /* data length */
578
579
    ctx->umf_start_offset = url_ftell(pb);
580
    gxf_write_umf_payload(pb, ctx);
581
    gxf_write_umf_material_description(pb, ctx);
582
    ctx->umf_track_size = gxf_write_umf_track_description(pb, ctx);
583
    ctx->umf_media_size = gxf_write_umf_media_description(pb, ctx);
584
    ctx->umf_user_data_size = gxf_write_umf_user_data(pb, ctx);
585
    ctx->umf_length = url_ftell(pb) - ctx->umf_start_offset;
586
    return updatePacketSize(pb, pos);
587
}
588
589 20121213 Baptiste Coudurier
#define GXF_NODELAY -5000
590
591 86329cce Baptiste Coudurier
static int gxf_write_header(AVFormatContext *s)
592
{
593 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
594 86329cce Baptiste Coudurier
    GXFContext *gxf = s->priv_data;
595
    int i;
596
597
    gxf->fc = s;
598
    gxf->flags |= 0x00080000; /* material is simple clip */
599
    for (i = 0; i < s->nb_streams; ++i) {
600
        AVStream *st = s->streams[i];
601
        GXFStreamContext *sc = &gxf->streams[i];
602
603
        sc->codec = st->codec;
604
        sc->index = i;
605
        sc->media_type = codec_get_tag(gxf_media_types, sc->codec->codec_id);
606
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
607
            if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
608
                av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
609
                return -1;
610
            }
611
            if (st->codec->sample_rate != 48000) {
612
                av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
613
                return -1;
614
            }
615
            if (st->codec->channels != 1) {
616
                av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
617
                return -1;
618
            }
619
            sc->track_type = 2;
620
            sc->sample_rate = st->codec->sample_rate;
621 201f1d45 Baptiste Coudurier
            av_set_pts_info(st, 64, 1, sc->sample_rate);
622 86329cce Baptiste Coudurier
            sc->sample_size = 16;
623
            sc->frame_rate_index = -2;
624
            sc->lines_index = -2;
625
            sc->fields = -2;
626
            gxf->audio_tracks++;
627
            gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
628 f5a478f6 Roman Shaposhnik
            av_fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
629 86329cce Baptiste Coudurier
        } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) {
630
            /* FIXME check from time_base ? */
631
            if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */
632
                sc->frame_rate_index = 5;
633
                sc->sample_rate = 60;
634
                gxf->flags |= 0x00000080;
635
            } else { /* assume PAL */
636
                sc->frame_rate_index = 6;
637
                sc->media_type++;
638
                sc->sample_rate = 50;
639
                gxf->flags |= 0x00000040;
640
            }
641
            gxf->sample_rate = sc->sample_rate;
642 20121213 Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
643
            sc->dts_delay = GXF_NODELAY;
644 86329cce Baptiste Coudurier
            if (gxf_find_lines_index(sc) < 0)
645
                sc->lines_index = -1;
646
            sc->sample_size = st->codec->bit_rate;
647
            sc->fields = 2; /* interlaced */
648
            switch (sc->codec->codec_id) {
649
            case CODEC_ID_MPEG2VIDEO:
650 afbbe5e3 Baptiste Coudurier
                sc->first_gop_closed = -1;
651 86329cce Baptiste Coudurier
                sc->track_type = 4;
652
                gxf->mpeg_tracks++;
653
                gxf->flags |= 0x00008000;
654
                break;
655
            case CODEC_ID_DVVIDEO:
656
                if (sc->codec->pix_fmt == PIX_FMT_YUV422P) {
657
                    sc->media_type += 2;
658
                    sc->track_type = 6;
659
                    gxf->flags |= 0x00002000;
660
                } else {
661
                    sc->track_type = 5;
662
                    gxf->flags |= 0x00001000;
663
                }
664
                break;
665
            default:
666 99fd05cb Baptiste Coudurier
                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
667 86329cce Baptiste Coudurier
                return -1;
668
            }
669
        }
670
    }
671
    gxf_write_map_packet(pb, gxf);
672
    //gxf_write_flt_packet(pb, gxf);
673
    gxf_write_umf_packet(pb, gxf);
674
    put_flush_packet(pb);
675
    return 0;
676
}
677
678
static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
679
{
680 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
681 86329cce Baptiste Coudurier
682
    gxf_write_packet_header(pb, PKT_EOS);
683
    return updatePacketSize(pb, pos);
684
}
685
686
static int gxf_write_trailer(AVFormatContext *s)
687
{
688 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
689 86329cce Baptiste Coudurier
    GXFContext *gxf = s->priv_data;
690 bc5c918e Diego Biurrun
    int64_t end;
691 86329cce Baptiste Coudurier
    int i;
692
693
    for (i = 0; i < s->nb_streams; ++i) {
694 507ff760 Baptiste Coudurier
        if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
695 f5a478f6 Roman Shaposhnik
            av_fifo_free(&gxf->streams[i].audio_buffer);
696 86329cce Baptiste Coudurier
    }
697
698
    gxf_write_eos_packet(pb, gxf);
699
    end = url_ftell(pb);
700
    url_fseek(pb, 0, SEEK_SET);
701
    /* overwrite map and umf packets with new values */
702
    gxf_write_map_packet(pb, gxf);
703
    //gxf_write_flt_packet(pb, gxf);
704
    gxf_write_umf_packet(pb, gxf);
705
    url_fseek(pb, end, SEEK_SET);
706
    return 0;
707
}
708
709 76c9cb0a Baptiste Coudurier
static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
710
{
711
    uint32_t c=-1;
712
    int i;
713
    for(i=0; i<size-4 && c!=0x100; i++){
714
        c = (c<<8) + buf[i];
715 afbbe5e3 Baptiste Coudurier
        if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
716 2b7e230b Baptiste Coudurier
            sc->first_gop_closed= (buf[i+4]>>6)&1;
717 76c9cb0a Baptiste Coudurier
    }
718
    return (buf[i+1]>>3)&7;
719
}
720
721 86329cce Baptiste Coudurier
static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size)
722
{
723
    GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
724 51d2ff80 Baptiste Coudurier
    int64_t dts = av_rescale_rnd(pkt->dts, ctx->sample_rate, sc->codec->time_base.den, AV_ROUND_UP);
725 86329cce Baptiste Coudurier
726
    put_byte(pb, sc->media_type);
727
    put_byte(pb, sc->index);
728 201f1d45 Baptiste Coudurier
    put_be32(pb, dts);
729 86329cce Baptiste Coudurier
    if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
730
        put_be16(pb, 0);
731
        put_be16(pb, size / 2);
732
    } else if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
733 76c9cb0a Baptiste Coudurier
        int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
734
        if (frame_type == FF_I_TYPE) {
735 86329cce Baptiste Coudurier
            put_byte(pb, 0x0d);
736
            sc->iframes++;
737 76c9cb0a Baptiste Coudurier
        } else if (frame_type == FF_B_TYPE) {
738 86329cce Baptiste Coudurier
            put_byte(pb, 0x0f);
739
            sc->bframes++;
740
        } else {
741
            put_byte(pb, 0x0e);
742
            sc->pframes++;
743
        }
744
        put_be24(pb, size);
745
    } else if (sc->codec->codec_id == CODEC_ID_DVVIDEO) {
746
        put_byte(pb, size / 4096);
747
        put_be24(pb, 0);
748
    } else
749
        put_be32(pb, size);
750 201f1d45 Baptiste Coudurier
    put_be32(pb, dts);
751 86329cce Baptiste Coudurier
    put_byte(pb, 1); /* flags */
752
    put_byte(pb, 0); /* reserved */
753
    return 16;
754
}
755
756
static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt)
757
{
758
    GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
759 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
760 86329cce Baptiste Coudurier
    int padding = 0;
761
762
    gxf_write_packet_header(pb, PKT_MEDIA);
763
    if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
764
        padding = 4 - pkt->size % 4;
765 3068f1c8 Baptiste Coudurier
    else if (sc->codec->codec_type == CODEC_TYPE_AUDIO)
766
        padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
767 86329cce Baptiste Coudurier
    gxf_write_media_preamble(pb, ctx, pkt, pkt->size + padding);
768 3068f1c8 Baptiste Coudurier
    put_buffer(pb, pkt->data, pkt->size);
769 86329cce Baptiste Coudurier
    gxf_write_padding(pb, padding);
770 507ff760 Baptiste Coudurier
771
    if (sc->codec->codec_type == CODEC_TYPE_VIDEO)
772
        ctx->nb_frames += 2; // count fields
773
774 86329cce Baptiste Coudurier
    return updatePacketSize(pb, pos);
775
}
776
777
static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
778
{
779
    GXFContext *gxf = s->priv_data;
780
781 899681cd Björn Axelsson
    gxf_write_media_packet(s->pb, gxf, pkt);
782
    put_flush_packet(s->pb);
783 86329cce Baptiste Coudurier
    return 0;
784
}
785
786 201f1d45 Baptiste Coudurier
static int gxf_new_audio_packet(GXFContext *gxf, GXFStreamContext *sc, AVPacket *pkt, int flush)
787
{
788 f5a478f6 Roman Shaposhnik
    int size = flush ? av_fifo_size(&sc->audio_buffer) : GXF_AUDIO_PACKET_SIZE;
789 201f1d45 Baptiste Coudurier
790
    if (!size)
791
        return 0;
792
    av_new_packet(pkt, size);
793 f5a478f6 Roman Shaposhnik
    av_fifo_read(&sc->audio_buffer, pkt->data, size);
794 201f1d45 Baptiste Coudurier
    pkt->stream_index = sc->index;
795
    pkt->dts = sc->current_dts;
796
    sc->current_dts += size / 2; /* we only support 16 bit pcm mono for now */
797
    return size;
798
}
799
800 86329cce Baptiste Coudurier
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
801
{
802
    GXFContext *gxf = s->priv_data;
803 201f1d45 Baptiste Coudurier
    AVPacket new_pkt;
804 86329cce Baptiste Coudurier
    int i;
805
806 201f1d45 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
807 6a287fd7 Baptiste Coudurier
        AVStream *st = s->streams[i];
808
        GXFStreamContext *sc = &gxf->streams[i];
809
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
810 201f1d45 Baptiste Coudurier
            if (pkt && pkt->stream_index == i) {
811 1234da4c Baptiste Coudurier
                av_fifo_generic_write(&sc->audio_buffer, pkt->data, pkt->size, NULL);
812 201f1d45 Baptiste Coudurier
                pkt = NULL;
813 86329cce Baptiste Coudurier
            }
814 f5a478f6 Roman Shaposhnik
            if (flush || av_fifo_size(&sc->audio_buffer) >= GXF_AUDIO_PACKET_SIZE) {
815 5b9d79bd Baptiste Coudurier
                if (!pkt && gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) {
816 201f1d45 Baptiste Coudurier
                    pkt = &new_pkt;
817
                    break; /* add pkt right now into list */
818
                }
819 86329cce Baptiste Coudurier
            }
820 20121213 Baptiste Coudurier
        } else if (pkt && pkt->stream_index == i) {
821
            if (sc->dts_delay == GXF_NODELAY) /* adjust dts if needed */
822
                sc->dts_delay = pkt->dts;
823 6a287fd7 Baptiste Coudurier
            pkt->dts -= sc->dts_delay;
824 86329cce Baptiste Coudurier
        }
825
    }
826 201f1d45 Baptiste Coudurier
    return av_interleave_packet_per_dts(s, out, pkt, flush);
827 86329cce Baptiste Coudurier
}
828
829
AVOutputFormat gxf_muxer = {
830
    "gxf",
831 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("GXF format"),
832 86329cce Baptiste Coudurier
    NULL,
833
    "gxf",
834
    sizeof(GXFContext),
835
    CODEC_ID_PCM_S16LE,
836
    CODEC_ID_MPEG2VIDEO,
837
    gxf_write_header,
838
    gxf_write_packet,
839
    gxf_write_trailer,
840
    0,
841
    NULL,
842
    gxf_interleave_packet,
843
};