2006-07-19 11:38:22 +02:00
|
|
|
/*
|
|
|
|
* GXF muxer.
|
2009-01-19 16:46:40 +01:00
|
|
|
* Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
|
2006-07-19 11:38:22 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
2007-07-13 19:15:56 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-07-19 11:38:22 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-07-19 11:38:22 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-07-13 19:15:56 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2006-07-19 11:38:22 +02:00
|
|
|
*
|
2007-07-13 19:15:56 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-10-07 17:30:46 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-07-19 11:38:22 +02:00
|
|
|
*/
|
|
|
|
|
2008-05-09 13:56:36 +02:00
|
|
|
#include "libavutil/fifo.h"
|
2006-07-19 11:38:22 +02:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "gxf.h"
|
|
|
|
#include "riff.h"
|
2009-02-08 05:33:53 +01:00
|
|
|
#include "audiointerleave.h"
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
#define GXF_AUDIO_PACKET_SIZE 65536
|
|
|
|
|
|
|
|
typedef struct GXFStreamContext {
|
2009-02-08 05:33:53 +01:00
|
|
|
AudioInterleaveContext aic;
|
2006-07-19 11:38:22 +02:00
|
|
|
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;
|
2009-02-12 00:49:54 +01:00
|
|
|
int b_per_i_or_p; ///< number of B frames per I frame or P frame
|
2006-08-21 17:14:36 +02:00
|
|
|
int first_gop_closed;
|
2006-07-19 11:38:22 +02:00
|
|
|
} GXFStreamContext;
|
|
|
|
|
|
|
|
typedef struct GXFContext {
|
2009-02-13 22:32:18 +01:00
|
|
|
uint32_t nb_fields;
|
2006-07-19 11:38:22 +02:00
|
|
|
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;
|
|
|
|
int sample_rate;
|
|
|
|
int flags;
|
|
|
|
} 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 },
|
|
|
|
};
|
|
|
|
|
2007-01-21 02:39:17 +01:00
|
|
|
static const AVCodecTag gxf_media_types[] = {
|
2006-07-19 11:38:22 +02:00
|
|
|
{ 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."
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_find_lines_index(AVStream *st)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFStreamContext *sc = st->priv_data;
|
2006-07-19 11:38:22 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 6; ++i) {
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->height == gxf_lines_tab[i].height) {
|
|
|
|
sc->lines_index = gxf_lines_tab[i].index;
|
2006-07-19 11:38:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-03 12:16:29 +02:00
|
|
|
static void gxf_write_padding(ByteIOContext *pb, int64_t to_pad)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2006-08-01 12:00:12 +02:00
|
|
|
for (; to_pad > 0; to_pad--) {
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-03 12:16:29 +02:00
|
|
|
static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t curpos;
|
2006-07-19 11:38:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-03 12:16:29 +02:00
|
|
|
static int64_t updateSize(ByteIOContext *pb, int64_t pos)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t curpos;
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-12-13 22:58:17 +01:00
|
|
|
static void gxf_write_packet_header(ByteIOContext *pb, GXFPktType type)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, AVStream *st)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFStreamContext *sc = st->priv_data;
|
2006-07-19 11:38:22 +02:00
|
|
|
char buffer[1024];
|
2009-01-20 10:25:50 +01:00
|
|
|
int size, starting_line;
|
2006-07-19 11:38:22 +02:00
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
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++;
|
2009-02-12 01:08:44 +01:00
|
|
|
}
|
2009-03-08 22:55:08 +01:00
|
|
|
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 */
|
2006-07-19 11:38:22 +02:00
|
|
|
}
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->height == 512 || st->codec->height == 608)
|
2009-01-20 10:25:50 +01:00
|
|
|
starting_line = 7; // VBI
|
2009-03-08 22:55:08 +01:00
|
|
|
else if (st->codec->height == 480)
|
2009-01-20 10:25:50 +01:00
|
|
|
starting_line = 20;
|
|
|
|
else
|
|
|
|
starting_line = 23; // default PAL
|
|
|
|
|
2006-07-19 11:38:22 +02:00
|
|
|
size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
|
2009-01-20 10:25:50 +01:00
|
|
|
"Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
|
2009-03-08 22:55:08 +01:00
|
|
|
(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,
|
|
|
|
starting_line, st->codec->height / 16);
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, TRACK_MPG_AUX);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, size + 1);
|
|
|
|
put_buffer(pb, (uint8_t *)buffer, size + 1);
|
|
|
|
return size + 3;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *sc)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_track_description(ByteIOContext *pb, AVStream *st)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFStreamContext *sc = st->priv_data;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos;
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
/* track description section */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_byte(pb, sc->media_type + 0x80);
|
|
|
|
put_byte(pb, st->index + 0xC0);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
pos = url_ftell(pb);
|
|
|
|
put_be16(pb, 0); /* size */
|
|
|
|
|
|
|
|
/* media file name */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, TRACK_NAME);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
|
|
|
|
put_tag(pb, ES_NAME_PATTERN);
|
2009-03-08 22:55:08 +01:00
|
|
|
put_be16(pb, sc->media_info);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 0);
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
|
2006-07-19 11:38:22 +02:00
|
|
|
/* auxiliary information */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, TRACK_AUX);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 8);
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->codec_id == CODEC_ID_NONE)
|
|
|
|
gxf_write_timecode_auxiliary(pb, sc);
|
2006-07-19 11:38:22 +02:00
|
|
|
else
|
|
|
|
put_le64(pb, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* file system version */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, TRACK_VER);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
|
|
|
put_be32(pb, 0);
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
|
|
|
|
gxf_write_mpeg_auxiliary(pb, st);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
/* frame rate */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, TRACK_FPS);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
2009-03-08 22:55:08 +01:00
|
|
|
put_be32(pb, sc->frame_rate_index);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
/* lines per frame */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, TRACK_LINES);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
2009-03-08 22:55:08 +01:00
|
|
|
put_be32(pb, sc->lines_index);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
/* fields per frame */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, TRACK_FPF);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
2009-03-08 22:55:08 +01:00
|
|
|
put_be32(pb, sc->fields);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
return updateSize(pb, pos);
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_material_data_section(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos;
|
2009-03-08 22:55:08 +01:00
|
|
|
const char *filename = strrchr(s->filename, '/');
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
pos = url_ftell(pb);
|
|
|
|
put_be16(pb, 0); /* size */
|
|
|
|
|
|
|
|
/* name */
|
|
|
|
if (filename)
|
|
|
|
filename++;
|
|
|
|
else
|
2009-03-08 22:55:08 +01:00
|
|
|
filename = s->filename;
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, MAT_NAME);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
|
|
|
|
put_tag(pb, SERVER_PATH);
|
|
|
|
put_tag(pb, filename);
|
|
|
|
put_byte(pb, 0);
|
|
|
|
|
|
|
|
/* first field */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, MAT_FIRST_FIELD);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
|
|
|
put_be32(pb, 0);
|
|
|
|
|
|
|
|
/* last field */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, MAT_LAST_FIELD);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
2009-03-08 22:55:08 +01:00
|
|
|
put_be32(pb, gxf->nb_fields);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
/* reserved */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, MAT_MARK_IN);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
|
|
|
put_be32(pb, 0);
|
|
|
|
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, MAT_MARK_OUT);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
2009-03-08 22:55:08 +01:00
|
|
|
put_be32(pb, gxf->nb_fields);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
/* estimated size */
|
2007-07-26 15:01:09 +02:00
|
|
|
put_byte(pb, MAT_SIZE);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 4);
|
|
|
|
put_be32(pb, url_fsize(pb) / 1024);
|
|
|
|
|
|
|
|
return updateSize(pb, pos);
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_track_description_section(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos;
|
2006-07-19 11:38:22 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
pos = url_ftell(pb);
|
|
|
|
put_be16(pb, 0); /* size */
|
2009-03-08 22:55:08 +01:00
|
|
|
for (i = 0; i < s->nb_streams; ++i)
|
|
|
|
gxf_write_track_description(pb, s->streams[i]);
|
2006-07-19 11:38:22 +02:00
|
|
|
return updateSize(pb, pos);
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_map_packet(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos = url_ftell(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
gxf_write_packet_header(pb, PKT_MAP);
|
|
|
|
|
|
|
|
/* preamble */
|
|
|
|
put_byte(pb, 0xE0); /* version */
|
|
|
|
put_byte(pb, 0xFF); /* reserved */
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf_write_material_data_section(s);
|
|
|
|
gxf_write_track_description_section(s);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
return updatePacketSize(pb, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_flt_packet(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos = url_ftell(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
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
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_material_description(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
|
|
|
|
2009-01-20 10:14:46 +01:00
|
|
|
// XXX drop frame
|
|
|
|
uint32_t timecode =
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf->nb_fields / (gxf->sample_rate * 3600) % 24 << 24 | // hours
|
|
|
|
gxf->nb_fields / (gxf->sample_rate * 60) % 60 << 16 | // minutes
|
|
|
|
gxf->nb_fields / gxf->sample_rate % 60 << 8 | // seconds
|
|
|
|
gxf->nb_fields % gxf->sample_rate; // fields
|
|
|
|
|
|
|
|
put_le32(pb, gxf->flags);
|
|
|
|
put_le32(pb, gxf->nb_fields); /* length of the longest track */
|
|
|
|
put_le32(pb, gxf->nb_fields); /* length of the shortest track */
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 0); /* mark in */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le32(pb, gxf->nb_fields); /* mark out */
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 0); /* timecode mark in */
|
2009-01-20 10:14:46 +01:00
|
|
|
put_le32(pb, timecode); /* timecode mark out */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le64(pb, s->timestamp); /* modification time */
|
|
|
|
put_le64(pb, s->timestamp); /* creation time */
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le16(pb, 0); /* reserved */
|
|
|
|
put_le16(pb, 0); /* reserved */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le16(pb, gxf->audio_tracks);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le16(pb, 0); /* timecode track count */
|
|
|
|
put_le16(pb, 0); /* reserved */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le16(pb, gxf->mpeg_tracks);
|
2006-07-19 11:38:22 +02:00
|
|
|
return 48;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_payload(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
|
|
|
|
|
|
|
put_le32(pb, gxf->umf_length); /* total length of the umf data */
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 3); /* version */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le32(pb, s->nb_streams);
|
|
|
|
put_le32(pb, gxf->umf_track_offset); /* umf track section offset */
|
|
|
|
put_le32(pb, gxf->umf_track_size);
|
|
|
|
put_le32(pb, s->nb_streams);
|
|
|
|
put_le32(pb, gxf->umf_media_offset);
|
|
|
|
put_le32(pb, gxf->umf_media_size);
|
2009-03-08 23:12:56 +01:00
|
|
|
put_le32(pb, gxf->umf_length); /* user data offset */
|
|
|
|
put_le32(pb, 0); /* user data size */
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 0); /* reserved */
|
|
|
|
put_le32(pb, 0); /* reserved */
|
|
|
|
return 48;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_track_description(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
|
|
|
GXFContext *gxf = s->priv_data;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos = url_ftell(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
int i;
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf->umf_track_offset = pos - gxf->umf_start_offset;
|
|
|
|
for (i = 0; i < s->nb_streams; ++i) {
|
2009-03-08 23:23:44 +01:00
|
|
|
GXFStreamContext *sc = s->streams[i]->priv_data;
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le16(pb, sc->media_info);
|
|
|
|
put_le16(pb, 1);
|
|
|
|
}
|
|
|
|
return url_ftell(pb) - pos;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_media_mpeg(ByteIOContext *pb, AVStream *st)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFStreamContext *sc = st->priv_data;
|
|
|
|
|
|
|
|
if (st->codec->pix_fmt == PIX_FMT_YUV422P)
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 2);
|
|
|
|
else
|
|
|
|
put_le32(pb, 1); /* default to 420 */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
|
|
|
|
put_le32(pb, 1); /* I picture per GOP */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le32(pb, sc->p_per_gop);
|
|
|
|
put_le32(pb, sc->b_per_i_or_p);
|
|
|
|
if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 2);
|
2009-03-08 22:55:08 +01:00
|
|
|
else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO)
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 1);
|
|
|
|
else
|
|
|
|
put_le32(pb, 0);
|
|
|
|
put_le32(pb, 0); /* reserved */
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *sc)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *sc)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
put_be32(pb, 0);
|
|
|
|
}
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *sc)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
|
|
|
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
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *sc)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_media_description(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos;
|
2006-07-19 11:38:22 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
pos = url_ftell(pb);
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf->umf_media_offset = pos - gxf->umf_start_offset;
|
|
|
|
for (i = 0; i < s->nb_streams; ++i) {
|
|
|
|
AVStream *st = s->streams[i];
|
|
|
|
GXFStreamContext *sc = st->priv_data;
|
2006-07-19 11:38:22 +02:00
|
|
|
char buffer[88];
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t startpos, curpos;
|
2006-07-19 11:38:22 +02:00
|
|
|
int path_size = strlen(ES_NAME_PATTERN);
|
|
|
|
|
2006-08-21 15:57:12 +02:00
|
|
|
memset(buffer, 0, 88);
|
2006-07-19 11:38:22 +02:00
|
|
|
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 */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le32(pb, gxf->nb_fields);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_le32(pb, 0); /* attributes rw, ro */
|
|
|
|
put_le32(pb, 0); /* mark in */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_le32(pb, gxf->nb_fields); /* mark out */
|
2006-07-19 11:38:22 +02:00
|
|
|
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 */
|
2009-03-08 22:55:08 +01:00
|
|
|
switch (st->codec->codec_id) {
|
2006-07-19 11:38:22 +02:00
|
|
|
case CODEC_ID_MPEG2VIDEO:
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf_write_umf_media_mpeg(pb, st);
|
2006-07-19 11:38:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_umf_packet(AVFormatContext *s)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos = url_ftell(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
gxf_write_packet_header(pb, PKT_UMF);
|
|
|
|
|
|
|
|
/* preamble */
|
|
|
|
put_byte(pb, 3); /* first and last (only) packet */
|
2009-03-08 22:55:08 +01:00
|
|
|
put_be32(pb, gxf->umf_length); /* data length */
|
|
|
|
|
|
|
|
gxf->umf_start_offset = url_ftell(pb);
|
|
|
|
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);
|
|
|
|
gxf->umf_length = url_ftell(pb) - gxf->umf_start_offset;
|
2006-07-19 11:38:22 +02:00
|
|
|
return updatePacketSize(pb, pos);
|
|
|
|
}
|
|
|
|
|
2009-02-08 05:33:53 +01:00
|
|
|
static const int GXF_samples_per_frame[] = { 32768, 0 };
|
|
|
|
|
2006-07-19 11:38:22 +02:00
|
|
|
static int gxf_write_header(AVFormatContext *s)
|
|
|
|
{
|
2007-11-21 08:41:00 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2006-07-19 11:38:22 +02:00
|
|
|
GXFContext *gxf = s->priv_data;
|
2009-03-08 23:23:44 +01:00
|
|
|
uint8_t tracks[255] = {0};
|
|
|
|
int i, media_info = 0;
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
gxf->flags |= 0x00080000; /* material is simple clip */
|
|
|
|
for (i = 0; i < s->nb_streams; ++i) {
|
|
|
|
AVStream *st = s->streams[i];
|
2009-01-31 12:06:20 +01:00
|
|
|
GXFStreamContext *sc = av_mallocz(sizeof(*sc));
|
|
|
|
if (!sc)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
st->priv_data = sc;
|
2006-07-19 11:38:22 +02:00
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
sc->media_type = codec_get_tag(gxf_media_types, st->codec->codec_id);
|
2006-07-19 11:38:22 +02:00
|
|
|
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;
|
2006-08-12 02:18:58 +02:00
|
|
|
av_set_pts_info(st, 64, 1, sc->sample_rate);
|
2006-07-19 11:38:22 +02:00
|
|
|
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 */
|
2009-03-08 23:23:44 +01:00
|
|
|
media_info = 'A';
|
2009-03-08 22:55:08 +01:00
|
|
|
} else if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
|
2006-07-19 11:38:22 +02:00
|
|
|
/* FIXME check from time_base ? */
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
|
2006-07-19 11:38:22 +02:00
|
|
|
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;
|
2007-02-14 15:54:20 +01:00
|
|
|
av_set_pts_info(st, 64, 1, st->codec->time_base.den);
|
2009-03-08 22:55:08 +01:00
|
|
|
if (gxf_find_lines_index(st) < 0)
|
2006-07-19 11:38:22 +02:00
|
|
|
sc->lines_index = -1;
|
|
|
|
sc->sample_size = st->codec->bit_rate;
|
|
|
|
sc->fields = 2; /* interlaced */
|
2009-03-08 22:55:08 +01:00
|
|
|
switch (st->codec->codec_id) {
|
2009-03-08 23:23:44 +01:00
|
|
|
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;
|
2006-07-19 11:38:22 +02:00
|
|
|
case CODEC_ID_MPEG2VIDEO:
|
2006-08-21 17:49:08 +02:00
|
|
|
sc->first_gop_closed = -1;
|
2006-07-19 11:38:22 +02:00
|
|
|
sc->track_type = 4;
|
|
|
|
gxf->mpeg_tracks++;
|
|
|
|
gxf->flags |= 0x00008000;
|
2009-03-08 23:23:44 +01:00
|
|
|
media_info = 'M';
|
2006-07-19 11:38:22 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_DVVIDEO:
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
|
2006-07-19 11:38:22 +02:00
|
|
|
sc->media_type += 2;
|
|
|
|
sc->track_type = 6;
|
|
|
|
gxf->flags |= 0x00002000;
|
2009-03-08 23:23:44 +01:00
|
|
|
media_info = 'E';
|
2006-07-19 11:38:22 +02:00
|
|
|
} else {
|
|
|
|
sc->track_type = 5;
|
|
|
|
gxf->flags |= 0x00001000;
|
2009-03-08 23:23:44 +01:00
|
|
|
media_info = 'D';
|
2006-07-19 11:38:22 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2006-10-18 17:10:14 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "video codec not supported\n");
|
2006-07-19 11:38:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2009-03-08 23:23:44 +01:00
|
|
|
/* 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]++);
|
2006-07-19 11:38:22 +02:00
|
|
|
}
|
2009-02-08 05:33:53 +01:00
|
|
|
|
|
|
|
if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf_write_map_packet(s);
|
|
|
|
//gxf_write_flt_packet(s);
|
|
|
|
gxf_write_umf_packet(s);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_flush_packet(pb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_eos_packet(ByteIOContext *pb)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos = url_ftell(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
gxf_write_packet_header(pb, PKT_EOS);
|
|
|
|
return updatePacketSize(pb, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gxf_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
2007-11-21 08:41:00 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t end;
|
2006-07-19 11:38:22 +02:00
|
|
|
|
2009-02-08 05:33:53 +01:00
|
|
|
ff_audio_interleave_close(s);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf_write_eos_packet(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
end = url_ftell(pb);
|
|
|
|
url_fseek(pb, 0, SEEK_SET);
|
|
|
|
/* overwrite map and umf packets with new values */
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf_write_map_packet(s);
|
|
|
|
//gxf_write_flt_packet(s);
|
|
|
|
gxf_write_umf_packet(s);
|
2006-07-19 11:38:22 +02:00
|
|
|
url_fseek(pb, end, SEEK_SET);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-09 12:48:32 +02:00
|
|
|
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];
|
2006-08-21 17:49:08 +02:00
|
|
|
if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
|
2006-08-21 17:14:36 +02:00
|
|
|
sc->first_gop_closed= (buf[i+4]>>6)&1;
|
2006-08-09 12:48:32 +02:00
|
|
|
}
|
|
|
|
return (buf[i+1]>>3)&7;
|
|
|
|
}
|
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
|
|
|
GXFStreamContext *sc = st->priv_data;
|
2009-02-13 22:36:36 +01:00
|
|
|
unsigned field_nb;
|
|
|
|
/* If the video is frame-encoded, the frame numbers shall be represented by
|
|
|
|
* even field numbers.
|
|
|
|
* see SMPTE360M-2004 6.4.2.1.3 Media field number */
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
|
|
|
|
field_nb = gxf->nb_fields;
|
2009-02-13 22:36:36 +01:00
|
|
|
} else {
|
2009-03-08 22:55:08 +01:00
|
|
|
field_nb = av_rescale_rnd(pkt->dts, gxf->sample_rate, st->codec->time_base.den, AV_ROUND_UP);
|
2009-02-13 22:36:36 +01:00
|
|
|
}
|
2006-07-19 11:38:22 +02:00
|
|
|
|
|
|
|
put_byte(pb, sc->media_type);
|
2009-03-08 22:55:08 +01:00
|
|
|
put_byte(pb, st->index);
|
2009-02-13 22:36:36 +01:00
|
|
|
put_be32(pb, field_nb);
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
|
2006-07-19 11:38:22 +02:00
|
|
|
put_be16(pb, 0);
|
|
|
|
put_be16(pb, size / 2);
|
2009-03-08 22:55:08 +01:00
|
|
|
} else if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
|
2006-08-09 12:48:32 +02:00
|
|
|
int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
|
|
|
|
if (frame_type == FF_I_TYPE) {
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 0x0d);
|
|
|
|
sc->iframes++;
|
2006-08-09 12:48:32 +02:00
|
|
|
} else if (frame_type == FF_B_TYPE) {
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 0x0f);
|
|
|
|
sc->bframes++;
|
|
|
|
} else {
|
|
|
|
put_byte(pb, 0x0e);
|
|
|
|
sc->pframes++;
|
|
|
|
}
|
|
|
|
put_be24(pb, size);
|
2009-03-08 22:55:08 +01:00
|
|
|
} else if (st->codec->codec_id == CODEC_ID_DVVIDEO) {
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, size / 4096);
|
|
|
|
put_be24(pb, 0);
|
|
|
|
} else
|
|
|
|
put_be32(pb, size);
|
2009-02-13 22:36:36 +01:00
|
|
|
put_be32(pb, field_nb);
|
2006-07-19 11:38:22 +02:00
|
|
|
put_byte(pb, 1); /* flags */
|
|
|
|
put_byte(pb, 0); /* reserved */
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
2009-03-08 23:14:26 +01:00
|
|
|
static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
|
2006-07-19 11:38:22 +02:00
|
|
|
{
|
2009-03-08 22:55:08 +01:00
|
|
|
GXFContext *gxf = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos = url_ftell(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
int padding = 0;
|
|
|
|
|
|
|
|
gxf_write_packet_header(pb, PKT_MEDIA);
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
|
2006-07-19 11:38:22 +02:00
|
|
|
padding = 4 - pkt->size % 4;
|
2009-03-08 22:55:08 +01:00
|
|
|
else if (st->codec->codec_type == CODEC_TYPE_AUDIO)
|
2006-08-01 11:56:27 +02:00
|
|
|
padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
|
2009-03-08 22:55:08 +01:00
|
|
|
gxf_write_media_preamble(s, pkt, pkt->size + padding);
|
2006-08-01 11:56:27 +02:00
|
|
|
put_buffer(pb, pkt->data, pkt->size);
|
2006-07-19 11:38:22 +02:00
|
|
|
gxf_write_padding(pb, padding);
|
2009-01-20 10:43:58 +01:00
|
|
|
|
2009-03-08 22:55:08 +01:00
|
|
|
if (st->codec->codec_type == CODEC_TYPE_VIDEO)
|
|
|
|
gxf->nb_fields += 2; // count fields
|
2009-01-20 10:43:58 +01:00
|
|
|
|
2009-03-08 23:14:26 +01:00
|
|
|
put_flush_packet(pb);
|
2006-07-19 11:38:22 +02:00
|
|
|
|
2009-03-08 23:14:26 +01:00
|
|
|
return updatePacketSize(pb, pos);
|
2006-07-19 11:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
|
|
|
|
{
|
2009-02-09 22:34:20 +01:00
|
|
|
return ff_audio_rechunk_interleave(s, out, pkt, flush,
|
2009-02-08 05:33:53 +01:00
|
|
|
av_interleave_packet_per_dts, ff_interleave_compare_dts);
|
2006-07-19 11:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AVOutputFormat gxf_muxer = {
|
|
|
|
"gxf",
|
2008-06-03 18:20:54 +02:00
|
|
|
NULL_IF_CONFIG_SMALL("GXF format"),
|
2006-07-19 11:38:22 +02:00
|
|
|
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,
|
|
|
|
};
|