b5f46e93 |
/*
* MXF muxer
* Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com> |
a1a37f13 |
* Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> |
b5f46e93 |
*
* This file is part of FFmpeg.
*
* 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.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1d42df72 |
* |
9a6d5810 |
* signal_standard, color_siting, store_user_comments and klv_fill_key version
* fixes sponsored by NOA GmbH |
b5f46e93 |
*/
/*
* References
* SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
* SMPTE 377M MXF File Format Specifications
* SMPTE 379M MXF Generic Container
* SMPTE 381M Mapping MPEG Streams into the MXF Generic Container |
c29d999f |
* SMPTE 422M Mapping JPEG 2000 Codestreams into the MXF Generic Container |
b5f46e93 |
* SMPTE RP210: SMPTE Metadata Dictionary
* SMPTE RP224: Registry of SMPTE Universal Labels
*/
|
a0d5ca4f |
#include <inttypes.h> |
33abc1a7 |
#include <math.h> |
f5b28890 |
#include <time.h> |
33abc1a7 |
|
b33ca05b |
#include "libavutil/opt.h" |
cafdc187 |
#include "libavutil/random_seed.h" |
bf6e83a8 |
#include "libavutil/timecode.h" |
db03f91f |
#include "libavutil/avassert.h" |
58afb312 |
#include "libavutil/pixdesc.h" |
82ee7d0d |
#include "libavutil/time_internal.h" |
251bf875 |
#include "libavcodec/bytestream.h" |
d99001ca |
#include "libavcodec/dnxhddata.h" |
2114e884 |
#include "libavcodec/h264.h"
#include "libavcodec/internal.h" |
f1544e79 |
#include "audiointerleave.h" |
251bf875 |
#include "avformat.h" |
95126728 |
#include "avio_internal.h" |
2c4d7bf0 |
#include "internal.h" |
e48891bd |
#include "mxf.h" |
135e6843 |
#include "config.h" |
e48891bd |
|
c6610a21 |
extern AVOutputFormat ff_mxf_d10_muxer; |
6a8a3bfb |
extern AVOutputFormat ff_mxf_opatom_muxer; |
bd3f81f3 |
|
99b170f8 |
#define EDIT_UNITS_PER_BODY 250 |
85d0d114 |
#define KAG_SIZE 512 |
0e9dbd96 |
|
daf8cf35 |
typedef struct MXFLocalTagPair { |
e48891bd |
int local_tag;
UID uid;
} MXFLocalTagPair;
|
daf8cf35 |
typedef struct MXFIndexEntry { |
0e9dbd96 |
uint8_t flags;
uint64_t offset; |
425c9962 |
unsigned slice_offset; ///< offset of audio slice |
29054fdd |
uint16_t temporal_ref; |
0e9dbd96 |
} MXFIndexEntry;
|
daf8cf35 |
typedef struct MXFStreamContext { |
cc812c8c |
AudioInterleaveContext aic; |
2c8d4388 |
UID track_essence_element_key; |
b399b932 |
int index; ///< index in mxf_essence_container_uls table |
4d418883 |
const UID *codec_ul; |
b399b932 |
int order; ///< interleaving order if dts are equal |
da9cea77 |
int interlaced; ///< whether picture is interlaced |
f6d952f5 |
int field_dominance; ///< tff=1, bff=2 |
d99001ca |
int component_depth; |
3b176af0 |
int color_siting; |
5a3d2541 |
int signal_standard; |
e32d832a |
int h_chroma_sub_sample; |
0e9dbd96 |
int temporal_reordering; |
e86a14de |
AVRational aspect_ratio; ///< display aspect ratio |
61f9e3c1 |
int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing |
52cf08b4 |
int video_bit_rate; |
2c8d4388 |
} MXFStreamContext;
|
daf8cf35 |
typedef struct MXFContainerEssenceEntry { |
b8ad3f78 |
UID container_ul;
UID element_ul; |
4d418883 |
UID codec_ul; |
6f2c523c |
void (*write_desc)(AVFormatContext *, AVStream *); |
a4d7a678 |
} MXFContainerEssenceEntry; |
b8ad3f78 |
|
2253d6d7 |
static const struct { |
36ef5369 |
enum AVCodecID id; |
2253d6d7 |
int index;
} mxf_essence_mappings[] = { |
36ef5369 |
{ AV_CODEC_ID_MPEG2VIDEO, 0 },
{ AV_CODEC_ID_PCM_S24LE, 1 },
{ AV_CODEC_ID_PCM_S16LE, 1 }, |
7a72695c |
{ AV_CODEC_ID_DVVIDEO, 15 },
{ AV_CODEC_ID_DNXHD, 24 }, |
c29d999f |
{ AV_CODEC_ID_JPEG2000, 34 }, |
2114e884 |
{ AV_CODEC_ID_H264, 35 }, |
36ef5369 |
{ AV_CODEC_ID_NONE } |
2253d6d7 |
};
|
f2ebd159 |
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st); |
baed2ec4 |
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st); |
f2ebd159 |
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st); |
bd3f81f3 |
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st); |
f2ebd159 |
|
a4d7a678 |
static const MXFContainerEssenceEntry mxf_essence_container_uls[] = { |
b8ad3f78 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, |
4d418883 |
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
f2ebd159 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 }, |
2253d6d7 |
mxf_write_mpegvideo_desc }, |
baed2ec4 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, |
2253d6d7 |
mxf_write_aes3_desc }, |
b8ad3f78 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, |
4d418883 |
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 }, |
f2ebd159 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, |
2253d6d7 |
mxf_write_wav_desc }, |
bd3f81f3 |
// D-10 625/50 PAL 50mb/s
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
mxf_write_cdci_desc },
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
mxf_write_generic_sound_desc },
// D-10 525/60 NTSC 50mb/s
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
mxf_write_cdci_desc },
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
mxf_write_generic_sound_desc },
// D-10 625/50 PAL 40mb/s
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
mxf_write_cdci_desc },
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
mxf_write_generic_sound_desc },
// D-10 525/60 NTSC 40mb/s
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
mxf_write_cdci_desc },
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
mxf_write_generic_sound_desc },
// D-10 625/50 PAL 30mb/s
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
mxf_write_cdci_desc },
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
mxf_write_generic_sound_desc },
// D-10 525/60 NTSC 30mb/s
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
mxf_write_cdci_desc },
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
mxf_write_generic_sound_desc }, |
d885cc41 |
// DV Unknown |
135e6843 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
mxf_write_cdci_desc },
// DV25 525/60
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
mxf_write_cdci_desc },
// DV25 625/50
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },
mxf_write_cdci_desc },
// DV50 525/60
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 },
mxf_write_cdci_desc },
// DV50 625/50
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },
mxf_write_cdci_desc },
// DV100 1080/60
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 },
mxf_write_cdci_desc },
// DV100 1080/50
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 },
mxf_write_cdci_desc },
// DV100 720/60
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 },
mxf_write_cdci_desc },
// DV100 720/50
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 },
mxf_write_cdci_desc }, |
d99001ca |
// DNxHD 1080p 10bit high
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 1080p 8bit medium
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 1080p 8bit high
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 1080i 10bit high
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 1080i 8bit medium
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 1080i 8bit high
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 720p 10bit
{ { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 720p 8bit high
{ { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 720p 8bit medium
{ { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc },
// DNxHD 720p 8bit low
{ { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
{ 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, |
b8a90976 |
{ 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 }, |
d99001ca |
mxf_write_cdci_desc }, |
c29d999f |
// JPEG2000
{ { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 },
{ 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x08,0x00 },
{ 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },
mxf_write_cdci_desc }, |
2114e884 |
// H.264
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x10,0x60,0x01 },
{ 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
mxf_write_mpegvideo_desc }, |
b8ad3f78 |
{ { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, |
4d418883 |
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, |
f2ebd159 |
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, |
2253d6d7 |
NULL }, |
b8ad3f78 |
};
|
2c8d4388 |
typedef struct MXFContext { |
b33ca05b |
AVClass *av_class; |
cc984060 |
int64_t footer_partition_offset; |
2c8d4388 |
int essence_container_count; |
cc812c8c |
AVRational time_base; |
7c7c1a76 |
int header_written; |
0e9dbd96 |
MXFIndexEntry *index_entries;
unsigned edit_units_count; |
f5b28890 |
uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8) |
425c9962 |
uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise) |
99b170f8 |
int last_indexed_edit_unit;
uint64_t *body_partition_offset;
unsigned body_partitions_count;
int last_key_index; ///< index of last key frame |
7c9668cf |
uint64_t duration; |
bf6e83a8 |
AVTimecode tc; ///< timecode context |
7c9668cf |
AVStream *timecode_track;
int timecode_base; ///< rounded time code base (25 or 30) |
bd3f81f3 |
int edit_unit_byte_count; ///< fixed edit unit byte count |
73d0869a |
uint64_t body_offset; |
cafdc187 |
uint32_t instance_number;
uint8_t umid[16]; ///< unique material identifier |
3eae34d5 |
int channel_count; |
7630cce4 |
int signal_standard; |
8d54850f |
uint32_t tagged_value_count; |
415f1fab |
AVRational audio_edit_rate; |
9a6d5810 |
int store_user_comments; |
2c8d4388 |
} MXFContext; |
fda0d4af |
|
e48891bd |
static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd }; |
cafdc187 |
static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 }; |
e48891bd |
/**
* complete key for operation pattern, partitions, and primer pack
*/ |
454d53ed |
static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 }; |
6a8a3bfb |
static const uint8_t opatom_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x02,0x01,0x10,0x03,0x00,0x00 }; |
6fb625c4 |
static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 }; |
6685f639 |
static const uint8_t header_open_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }; // OpenIncomplete
static const uint8_t header_closed_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }; // ClosedComplete |
2e92a27a |
static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 }; |
99b170f8 |
static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete |
6685f639 |
|
fda0d4af |
/**
* partial key for header metadata
*/
static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 }; |
6fb625c4 |
static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 }; |
fda0d4af |
|
9b9466b0 |
/**
* SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
*/
static const MXFLocalTagPair mxf_local_tag_batch[] = {
// preface set
{ 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
{ 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
{ 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
{ 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
{ 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
{ 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
{ 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
{ 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
// Identification
{ 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
{ 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
{ 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */ |
faac3f0f |
{ 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */ |
9b9466b0 |
{ 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
{ 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
// Content Storage
{ 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */ |
6e4668b8 |
{ 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */ |
9b9466b0 |
// Essence Container Data
{ 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
{ 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
// Package
{ 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
{ 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
{ 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */ |
001b28b0 |
{ 0x4402, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Package Name */ |
9b9466b0 |
{ 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
{ 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
// Track
{ 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */ |
316b438f |
{ 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */ |
9b9466b0 |
{ 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
{ 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
{ 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
// Sequence
{ 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
{ 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
{ 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
// Source Clip |
bd5c94b5 |
{ 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */ |
9b9466b0 |
{ 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
{ 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */ |
7c9668cf |
// Timecode Component
{ 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
{ 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
{ 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */ |
5b9bb66d |
// File Descriptor
{ 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */ |
9b9466b0 |
{ 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
{ 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */ |
001b28b0 |
{ 0x3002, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x02,0x00,0x00,0x00,0x00}}, /* ContainerDuration */ |
5b9bb66d |
{ 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
// Generic Picture Essence Descriptor |
4118d68f |
{ 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */ |
bd5c94b5 |
{ 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */ |
5b9bb66d |
{ 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
{ 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */ |
724e7d8d |
{ 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
{ 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */ |
ac9919b9 |
{ 0x320B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0E,0x00,0x00,0x00}}, /* Presentation Y offset */ |
5b9bb66d |
{ 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
{ 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */ |
f6d952f5 |
{ 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */ |
5a3d2541 |
{ 0x3215, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x05,0x01,0x13,0x00,0x00,0x00,0x00}}, /* Signal Standard */ |
ee1f7f4b |
// CDCI Picture Essence Descriptor
{ 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
{ 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */ |
3b176af0 |
{ 0x3303, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x06,0x00,0x00,0x00}}, /* Color Siting */ |
5b9bb66d |
// Generic Sound Essence Descriptor |
bd5c94b5 |
{ 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */ |
5b9bb66d |
{ 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
{ 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
{ 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */ |
0df4903a |
{ 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */ |
0e9dbd96 |
// Index Table Segment
{ 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
{ 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
{ 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */ |
bd5c94b5 |
{ 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */ |
0e9dbd96 |
{ 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
{ 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
{ 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
{ 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */ |
cbd70c41 |
// MPEG video Descriptor
{ 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */ |
6a43aab0 |
{ 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */ |
f9303ffd |
// Wave Audio Essence Descriptor
{ 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
{ 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */ |
9b9466b0 |
};
|
9a6d5810 |
static const MXFLocalTagPair mxf_user_comments_local_tag[] = {
{ 0x4406, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0C,0x00,0x00,0x00}}, /* User Comments */
{ 0x5001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x09,0x01,0x00,0x00}}, /* Name */
{ 0x5003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0A,0x01,0x00,0x00}}, /* Value */
};
|
ae628ec1 |
static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value) |
e48891bd |
{ |
77eb5504 |
avio_write(pb, uuid_base, 12);
avio_wb16(pb, type);
avio_wb16(pb, value); |
e48891bd |
}
|
cafdc187 |
static void mxf_write_umid(AVFormatContext *s, int type) |
2c8d4388 |
{ |
cafdc187 |
MXFContext *mxf = s->priv_data; |
77eb5504 |
avio_write(s->pb, umid_ul, 13);
avio_wb24(s->pb, mxf->instance_number);
avio_write(s->pb, mxf->umid, 15);
avio_w8(s->pb, type); |
2c8d4388 |
} |
fda0d4af |
|
ae628ec1 |
static void mxf_write_refs_count(AVIOContext *pb, int ref_count) |
fda0d4af |
{ |
77eb5504 |
avio_wb32(pb, ref_count);
avio_wb32(pb, 16); |
fda0d4af |
}
|
85d0d114 |
static int klv_ber_length(uint64_t len)
{
if (len < 128)
return 1;
else
return (av_log2(len) >> 3) + 2;
}
|
ae628ec1 |
static int klv_encode_ber_length(AVIOContext *pb, uint64_t len) |
e48891bd |
{
// Determine the best BER size
int size;
if (len < 128) {
//short form |
77eb5504 |
avio_w8(pb, len); |
e48891bd |
return 1;
}
size = (av_log2(len) >> 3) + 1;
// long form |
77eb5504 |
avio_w8(pb, 0x80 + size); |
e48891bd |
while(size) { |
6fb625c4 |
size--; |
77eb5504 |
avio_w8(pb, len >> 8 * size & 0xff); |
e48891bd |
}
return 0;
}
|
ae628ec1 |
static void klv_encode_ber4_length(AVIOContext *pb, int len) |
baaf7b1b |
{ |
77eb5504 |
avio_w8(pb, 0x80 + 3);
avio_wb24(pb, len); |
baaf7b1b |
}
|
6a8a3bfb |
static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
{
avio_w8(pb, 0x80 + 8);
avio_wb64(pb, len);
}
|
c18d208a |
/* |
d4ffc57d |
* Get essence container ul index |
c18d208a |
*/ |
36ef5369 |
static int mxf_get_essence_container_ul_index(enum AVCodecID id) |
b5f46e93 |
{ |
d4ffc57d |
int i; |
2253d6d7 |
for (i = 0; mxf_essence_mappings[i].id; i++)
if (mxf_essence_mappings[i].id == id)
return mxf_essence_mappings[i].index; |
d4ffc57d |
return -1; |
b5f46e93 |
}
|
9b9466b0 |
static void mxf_write_primer_pack(AVFormatContext *s) |
e48891bd |
{ |
9a6d5810 |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
e48891bd |
int local_tag_number, i = 0;
|
37d3e066 |
local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch); |
9a6d5810 |
local_tag_number += mxf->store_user_comments * FF_ARRAY_ELEMS(mxf_user_comments_local_tag); |
e48891bd |
|
77eb5504 |
avio_write(pb, primer_pack_key, 16); |
e48891bd |
klv_encode_ber_length(pb, local_tag_number * 18 + 8);
|
77eb5504 |
avio_wb32(pb, local_tag_number); // local_tag num
avio_wb32(pb, 18); // item size, always 18 according to the specs |
e48891bd |
|
9a6d5810 |
for (i = 0; i < FF_ARRAY_ELEMS(mxf_local_tag_batch); i++) { |
77eb5504 |
avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
avio_write(pb, mxf_local_tag_batch[i].uid, 16); |
e48891bd |
} |
9a6d5810 |
if (mxf->store_user_comments)
for (i = 0; i < FF_ARRAY_ELEMS(mxf_user_comments_local_tag); i++) {
avio_wb16(pb, mxf_user_comments_local_tag[i].local_tag);
avio_write(pb, mxf_user_comments_local_tag[i].uid, 16);
} |
e48891bd |
}
|
ae628ec1 |
static void mxf_write_local_tag(AVIOContext *pb, int size, int tag) |
e48891bd |
{ |
77eb5504 |
avio_wb16(pb, tag);
avio_wb16(pb, size); |
e48891bd |
}
|
ae628ec1 |
static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value) |
2c8d4388 |
{ |
77eb5504 |
avio_write(pb, header_metadata_key, 13);
avio_wb24(pb, value); |
2c8d4388 |
}
|
b5f46e93 |
static void mxf_free(AVFormatContext *s)
{
int i;
for (i = 0; i < s->nb_streams; i++) { |
ac0ab12f |
AVStream *st = s->streams[i]; |
b5f46e93 |
av_freep(&st->priv_data);
}
}
|
5ac4d768 |
static const MXFCodecUL *mxf_get_data_definition_ul(int type) |
b5f46e93 |
{ |
5ac4d768 |
const MXFCodecUL *uls = ff_mxf_data_definition_uls;
while (uls->uid[0]) {
if (type == uls->id) |
b5f46e93 |
break; |
a8f6e362 |
uls++; |
b5f46e93 |
}
return uls;
}
|
5c108092 |
//one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
#define DESCRIPTOR_COUNT(essence_container_count) \
(essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
|
3eaa53b3 |
static void mxf_write_essence_container_refs(AVFormatContext *s) |
cedebde1 |
{ |
c18d208a |
MXFContext *c = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
c18d208a |
int i; |
cedebde1 |
|
5c108092 |
mxf_write_refs_count(pb, DESCRIPTOR_COUNT(c->essence_container_count)); |
e7daab1e |
av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
for (i = 0; i < c->essence_container_count; i++) { |
844d9e41 |
MXFStreamContext *sc = s->streams[i]->priv_data; |
77eb5504 |
avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); |
e7daab1e |
} |
5c108092 |
if (c->essence_container_count > 1)
avio_write(pb, multiple_desc_ul, 16); |
cedebde1 |
}
|
9b9466b0 |
static void mxf_write_preface(AVFormatContext *s)
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
9b9466b0 |
mxf_write_metadata_key(pb, 0x012f00);
PRINT_KEY(s, "preface key", pb->buf_ptr - 16); |
5c108092 |
klv_encode_ber_length(pb, 130 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count)); |
9b9466b0 |
// write preface set uid
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, Preface, 0);
PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
|
f5b28890 |
// last modified date |
9b9466b0 |
mxf_write_local_tag(pb, 8, 0x3B02); |
77eb5504 |
avio_wb64(pb, mxf->timestamp); |
9b9466b0 |
// write version
mxf_write_local_tag(pb, 2, 0x3B05); |
77eb5504 |
avio_wb16(pb, 258); // v1.2 |
9b9466b0 |
// write identification_refs
mxf_write_local_tag(pb, 16 + 8, 0x3B06);
mxf_write_refs_count(pb, 1);
mxf_write_uuid(pb, Identification, 0);
// write content_storage_refs
mxf_write_local_tag(pb, 16, 0x3B03);
mxf_write_uuid(pb, ContentStorage, 0);
|
216b6631 |
// operational pattern |
9b9466b0 |
mxf_write_local_tag(pb, 16, 0x3B09); |
6a8a3bfb |
if (s->oformat == &ff_mxf_opatom_muxer)
avio_write(pb, opatom_ul, 16);
else
avio_write(pb, op1a_ul, 16); |
9b9466b0 |
// write essence_container_refs |
5c108092 |
mxf_write_local_tag(pb, 8 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A); |
3eaa53b3 |
mxf_write_essence_container_refs(s); |
9b9466b0 |
// write dm_scheme_refs
mxf_write_local_tag(pb, 8, 0x3B0B); |
77eb5504 |
avio_wb64(pb, 0); |
9b9466b0 |
}
|
5f94b920 |
/* |
399e3141 |
* Returns the length of the UTF-16 string, in 16-bit characters, that would result
* from decoding the utf-8 string.
*/
static uint64_t mxf_utf16len(const char *utf8_str)
{
const uint8_t *q = utf8_str;
uint64_t size = 0;
while (*q) {
uint32_t ch;
GET_UTF8(ch, *q++, goto invalid;)
if (ch < 0x10000)
size++;
else
size += 2;
continue;
invalid:
av_log(NULL, AV_LOG_ERROR, "Invaid UTF8 sequence in mxf_utf16len\n\n");
}
size += 1;
return size;
}
/*
* Returns the calculated length a local tag containing an utf-8 string as utf-16
*/
static int mxf_utf16_local_tag_length(const char *utf8_str)
{
uint64_t size;
if (!utf8_str)
return 0;
size = mxf_utf16len(utf8_str);
if (size >= UINT16_MAX/2) {
av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
return 0;
}
return 4 + size * 2;
}
/*
* Write a local tag containing an utf-8 string as utf-16 |
5f94b920 |
*/ |
ae628ec1 |
static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value) |
5f94b920 |
{ |
399e3141 |
uint64_t size = mxf_utf16len(value);
if (size >= UINT16_MAX/2) {
av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
return;
}
|
9fedbc89 |
mxf_write_local_tag(pb, size*2, tag); |
399e3141 |
avio_put_str16be(pb, value); |
5f94b920 |
}
|
9b9466b0 |
static void mxf_write_identification(AVFormatContext *s)
{ |
f5b28890 |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
2d2b5a14 |
const char *company = "FFmpeg"; |
618021ea |
const char *product = s->oformat != &ff_mxf_opatom_muxer ? "OP1a Muxer" : "OPAtom Muxer"; |
283a02c5 |
const char *version; |
6fdb4fb0 |
int length; |
9b9466b0 |
mxf_write_metadata_key(pb, 0x013000);
PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
|
0c1959b0 |
version = s->flags & AVFMT_FLAG_BITEXACT ? |
0c0c4d64 |
"0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION); |
399e3141 |
length = 72 + mxf_utf16_local_tag_length(company) +
mxf_utf16_local_tag_length(product) +
mxf_utf16_local_tag_length(version); |
9b9466b0 |
klv_encode_ber_length(pb, length);
// write uid
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, Identification, 0);
PRINT_KEY(s, "identification uid", pb->buf_ptr - 16); |
0b62964f |
|
9b9466b0 |
// write generation uid
mxf_write_local_tag(pb, 16, 0x3C09);
mxf_write_uuid(pb, Identification, 1); |
6fdb4fb0 |
mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name |
283a02c5 |
mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String |
9b9466b0 |
// write product uid
mxf_write_local_tag(pb, 16, 0x3C05);
mxf_write_uuid(pb, Identification, 2);
|
5ccad11d |
// modification date |
9b9466b0 |
mxf_write_local_tag(pb, 8, 0x3C06); |
77eb5504 |
avio_wb64(pb, mxf->timestamp); |
9b9466b0 |
}
static void mxf_write_content_storage(AVFormatContext *s)
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
9b9466b0 |
mxf_write_metadata_key(pb, 0x011800);
PRINT_KEY(s, "content storage key", pb->buf_ptr - 16); |
6e4668b8 |
klv_encode_ber_length(pb, 92); |
9b9466b0 |
// write uid
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, ContentStorage, 0);
PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16); |
a8f6e362 |
|
9b9466b0 |
// write package reference
mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
mxf_write_refs_count(pb, 2);
mxf_write_uuid(pb, MaterialPackage, 0);
mxf_write_uuid(pb, SourcePackage, 0); |
6e4668b8 |
// write essence container data
mxf_write_local_tag(pb, 8 + 16, 0x1902);
mxf_write_refs_count(pb, 1);
mxf_write_uuid(pb, EssenceContainerData, 0); |
9b9466b0 |
}
|
064abb8a |
static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) |
6986662b |
{ |
5a897cfa |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
ac0ab12f |
MXFStreamContext *sc = st->priv_data; |
6986662b |
mxf_write_metadata_key(pb, 0x013b00);
PRINT_KEY(s, "track key", pb->buf_ptr - 16);
klv_encode_ber_length(pb, 80);
// write track uid
mxf_write_local_tag(pb, 16, 0x3C0A); |
064abb8a |
mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index); |
6986662b |
PRINT_KEY(s, "track uid", pb->buf_ptr - 16); |
a8f6e362 |
|
6986662b |
// write track id
mxf_write_local_tag(pb, 4, 0x4801); |
77eb5504 |
avio_wb32(pb, st->index+2); |
6986662b |
|
b8ad3f78 |
// write track number |
6986662b |
mxf_write_local_tag(pb, 4, 0x4804); |
b8ad3f78 |
if (type == MaterialPackage) |
77eb5504 |
avio_wb32(pb, 0); // track number of material package is 0 |
b8ad3f78 |
else |
77eb5504 |
avio_write(pb, sc->track_essence_element_key + 12, 4); |
6986662b |
mxf_write_local_tag(pb, 8, 0x4B01); |
415f1fab |
if (st == mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer){
avio_wb32(pb, mxf->tc.rate.num);
avio_wb32(pb, mxf->tc.rate.den);
} else {
avio_wb32(pb, mxf->time_base.den);
avio_wb32(pb, mxf->time_base.num);
} |
6986662b |
// write origin
mxf_write_local_tag(pb, 8, 0x4B02); |
77eb5504 |
avio_wb64(pb, 0); |
6986662b |
// write sequence refs
mxf_write_local_tag(pb, 16, 0x4803); |
064abb8a |
mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index); |
6986662b |
}
|
7c9668cf |
static const uint8_t smpte_12m_timecode_track_data_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x01,0x01,0x00,0x00,0x00 };
static void mxf_write_common_fields(AVFormatContext *s, AVStream *st) |
fda0d4af |
{ |
7c9668cf |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
cdedd878 |
|
fda0d4af |
// find data define uls
mxf_write_local_tag(pb, 16, 0x0201); |
7c9668cf |
if (st == mxf->timecode_track) |
77eb5504 |
avio_write(pb, smpte_12m_timecode_track_data_ul, 16); |
7c9668cf |
else {
const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type); |
77eb5504 |
avio_write(pb, data_def_ul->uid, 16); |
7c9668cf |
} |
fda0d4af |
// write duration
mxf_write_local_tag(pb, 8, 0x0202); |
415f1fab |
if (st != mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer && st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
} else {
avio_wb64(pb, mxf->duration);
} |
fda0d4af |
}
|
064abb8a |
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) |
6986662b |
{ |
7c9668cf |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
7c9668cf |
enum MXFMetadataSetType component; |
6986662b |
mxf_write_metadata_key(pb, 0x010f00);
PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
klv_encode_ber_length(pb, 80);
mxf_write_local_tag(pb, 16, 0x3C0A); |
064abb8a |
mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index); |
6986662b |
PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16); |
7c9668cf |
mxf_write_common_fields(s, st); |
6986662b |
// write structural component
mxf_write_local_tag(pb, 16 + 8, 0x1001);
mxf_write_refs_count(pb, 1); |
7c9668cf |
if (st == mxf->timecode_track)
component = TimecodeComponent;
else |
659713bf |
component = SourceClip;
if (type == SourcePackage)
component += TypeBottom; |
7c9668cf |
mxf_write_uuid(pb, component, st->index);
}
|
659713bf |
static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) |
7c9668cf |
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
7c9668cf |
mxf_write_metadata_key(pb, 0x011400);
klv_encode_ber_length(pb, 75);
// UID
mxf_write_local_tag(pb, 16, 0x3C0A); |
659713bf |
mxf_write_uuid(pb, type == MaterialPackage ? TimecodeComponent :
TimecodeComponent + TypeBottom, st->index); |
7c9668cf |
mxf_write_common_fields(s, st);
// Start Time Code
mxf_write_local_tag(pb, 8, 0x1501); |
b33ca05b |
avio_wb64(pb, mxf->tc.start); |
7c9668cf |
// Rounded Time Code Base
mxf_write_local_tag(pb, 2, 0x1502); |
77eb5504 |
avio_wb16(pb, mxf->timecode_base); |
7c9668cf |
// Drop Frame
mxf_write_local_tag(pb, 1, 0x1503); |
bf6e83a8 |
avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME)); |
6986662b |
}
|
064abb8a |
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) |
6986662b |
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
6986662b |
int i;
mxf_write_metadata_key(pb, 0x011100);
PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
klv_encode_ber_length(pb, 108);
// write uid
mxf_write_local_tag(pb, 16, 0x3C0A); |
064abb8a |
mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index); |
6986662b |
PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16); |
7c9668cf |
mxf_write_common_fields(s, st); |
6986662b |
// write start_position
mxf_write_local_tag(pb, 8, 0x1201); |
77eb5504 |
avio_wb64(pb, 0); |
6986662b |
|
a8f6e362 |
// write source package uid, end of the reference |
6986662b |
mxf_write_local_tag(pb, 32, 0x1101);
if (type == SourcePackage) { |
a8f6e362 |
for (i = 0; i < 4; i++) |
77eb5504 |
avio_wb64(pb, 0); |
6986662b |
} else |
cafdc187 |
mxf_write_umid(s, 1); |
6986662b |
|
a8f6e362 |
// write source track id |
6986662b |
mxf_write_local_tag(pb, 4, 0x1102);
if (type == SourcePackage) |
77eb5504 |
avio_wb32(pb, 0); |
6986662b |
else |
77eb5504 |
avio_wb32(pb, st->index+2); |
6986662b |
}
|
9b9466b0 |
static void mxf_write_multi_descriptor(AVFormatContext *s)
{ |
034fcddf |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
bd3f81f3 |
const uint8_t *ul; |
9b9466b0 |
int i;
mxf_write_metadata_key(pb, 0x014400);
PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16); |
e3ba817b |
klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams); |
9b9466b0 |
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, MultipleDescriptor, 0);
PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
// write sample rate
mxf_write_local_tag(pb, 8, 0x3001); |
77eb5504 |
avio_wb32(pb, mxf->time_base.den);
avio_wb32(pb, mxf->time_base.num); |
9b9466b0 |
// write essence container ul
mxf_write_local_tag(pb, 16, 0x3004); |
bd3f81f3 |
if (mxf->essence_container_count > 1)
ul = multiple_desc_ul; |
844d9e41 |
else {
MXFStreamContext *sc = s->streams[0]->priv_data;
ul = mxf_essence_container_uls[sc->index].container_ul;
} |
77eb5504 |
avio_write(pb, ul, 16); |
9b9466b0 |
// write sub descriptor refs
mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
mxf_write_refs_count(pb, s->nb_streams); |
a8f6e362 |
for (i = 0; i < s->nb_streams; i++) |
9b9466b0 |
mxf_write_uuid(pb, SubDescriptor, i);
}
|
034fcddf |
static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size) |
fda0d4af |
{ |
034fcddf |
MXFContext *mxf = s->priv_data; |
a2f55f22 |
MXFStreamContext *sc = st->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
fda0d4af |
|
77eb5504 |
avio_write(pb, key, 16); |
76b092e2 |
klv_encode_ber4_length(pb, size+20+8+12+20); |
fda0d4af |
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, SubDescriptor, st->index);
mxf_write_local_tag(pb, 4, 0x3006); |
77eb5504 |
avio_wb32(pb, st->index+2); |
fda0d4af |
|
9b9466b0 |
mxf_write_local_tag(pb, 8, 0x3001); |
77eb5504 |
avio_wb32(pb, mxf->time_base.den);
avio_wb32(pb, mxf->time_base.num); |
9b9466b0 |
|
fda0d4af |
mxf_write_local_tag(pb, 16, 0x3004); |
77eb5504 |
avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); |
fda0d4af |
}
|
f2ebd159 |
static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }; |
baed2ec4 |
static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }; |
bd3f81f3 |
static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }; |
f2ebd159 |
|
db6db7e5 |
static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) |
fda0d4af |
{ |
383dde71 |
MXFStreamContext *sc = st->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
24d6a613 |
int stored_height = (st->codec->height+15)/16*16; |
6249143d |
int display_height; |
74334ae1 |
int f1, f2; |
3b176af0 |
unsigned desc_size = size+8+8+8+8+8+8+8+5+16+sc->interlaced*4+12+20+5; |
f6d952f5 |
if (sc->interlaced && sc->field_dominance)
desc_size += 5; |
5a3d2541 |
if (sc->signal_standard)
desc_size += 5; |
fda0d4af |
|
f6d952f5 |
mxf_write_generic_desc(s, st, key, desc_size); |
fda0d4af |
mxf_write_local_tag(pb, 4, 0x3203); |
77eb5504 |
avio_wb32(pb, st->codec->width); |
fda0d4af |
mxf_write_local_tag(pb, 4, 0x3202); |
77eb5504 |
avio_wb32(pb, stored_height>>sc->interlaced); |
fda0d4af |
|
724e7d8d |
mxf_write_local_tag(pb, 4, 0x3209); |
77eb5504 |
avio_wb32(pb, st->codec->width); |
724e7d8d |
|
6249143d |
if (st->codec->height == 608) // PAL + VBI
display_height = 576;
else if (st->codec->height == 512) // NTSC + VBI
display_height = 486;
else
display_height = st->codec->height;
|
724e7d8d |
mxf_write_local_tag(pb, 4, 0x3208); |
77eb5504 |
avio_wb32(pb, display_height>>sc->interlaced); |
724e7d8d |
|
ac9919b9 |
// presentation Y offset
mxf_write_local_tag(pb, 4, 0x320B);
avio_wb32(pb, (st->codec->height - display_height)>>sc->interlaced);
|
ee1f7f4b |
// component depth
mxf_write_local_tag(pb, 4, 0x3301); |
d99001ca |
avio_wb32(pb, sc->component_depth); |
ee1f7f4b |
// horizontal subsampling
mxf_write_local_tag(pb, 4, 0x3302); |
e32d832a |
avio_wb32(pb, sc->h_chroma_sub_sample); |
ee1f7f4b |
|
3b176af0 |
// color siting
mxf_write_local_tag(pb, 1, 0x3303);
avio_w8(pb, sc->color_siting);
|
5a3d2541 |
if (sc->signal_standard) {
mxf_write_local_tag(pb, 1, 0x3215);
avio_w8(pb, sc->signal_standard);
}
|
4118d68f |
// frame layout
mxf_write_local_tag(pb, 1, 0x320C); |
77eb5504 |
avio_w8(pb, sc->interlaced); |
4118d68f |
|
74334ae1 |
// video line map
switch (st->codec->height) { |
7a72695c |
case 576: f1 = 23; f2 = st->codec->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break; |
74334ae1 |
case 608: f1 = 7; f2 = 320; break; |
7a72695c |
case 480: f1 = 20; f2 = st->codec->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break; |
74334ae1 |
case 512: f1 = 7; f2 = 270; break;
case 720: f1 = 26; f2 = 0; break; // progressive
case 1080: f1 = 21; f2 = 584; break;
default: f1 = 0; f2 = 0; break;
}
if (!sc->interlaced) {
f2 = 0;
f1 *= 2;
}
|
ed00d0c1 |
mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D); |
77eb5504 |
avio_wb32(pb, sc->interlaced ? 2 : 1);
avio_wb32(pb, 4);
avio_wb32(pb, f1); |
8ed82d81 |
if (sc->interlaced) |
77eb5504 |
avio_wb32(pb, f2); |
74334ae1 |
|
fda0d4af |
mxf_write_local_tag(pb, 8, 0x320E); |
77eb5504 |
avio_wb32(pb, sc->aspect_ratio.num);
avio_wb32(pb, sc->aspect_ratio.den); |
221ef8ca |
mxf_write_local_tag(pb, 16, 0x3201); |
77eb5504 |
avio_write(pb, *sc->codec_ul, 16); |
f6d952f5 |
if (sc->interlaced && sc->field_dominance) {
mxf_write_local_tag(pb, 1, 0x3212);
avio_w8(pb, sc->field_dominance);
}
|
fda0d4af |
}
|
bd3f81f3 |
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
{ |
42c6850e |
mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 0); |
bd3f81f3 |
}
|
db6db7e5 |
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
52cf08b4 |
MXFStreamContext *sc = st->priv_data; |
6a43aab0 |
int profile_and_level = (st->codec->profile<<4) | st->codec->level; |
db6db7e5 |
|
2114e884 |
if (st->codec->codec_id != AV_CODEC_ID_H264) { |
d1b5ad39 |
mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 8+5); |
db6db7e5 |
|
d1b5ad39 |
// bit rate
mxf_write_local_tag(pb, 4, 0x8000);
avio_wb32(pb, sc->video_bit_rate); |
6a43aab0 |
|
d1b5ad39 |
// profile and level
mxf_write_local_tag(pb, 1, 0x8007);
if (!st->codec->profile)
profile_and_level |= 0x80; // escape bit
avio_w8(pb, profile_and_level); |
2114e884 |
} else {
mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 0);
} |
db6db7e5 |
}
static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) |
fda0d4af |
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
3eae34d5 |
MXFContext *mxf = s->priv_data;
int show_warnings = !mxf->footer_partition_offset; |
415f1fab |
int duration_size = 0;
if (s->oformat == &ff_mxf_opatom_muxer)
duration_size = 12; |
fda0d4af |
|
415f1fab |
mxf_write_generic_desc(s, st, key, size+duration_size+5+12+8+8);
if (duration_size > 0){
mxf_write_local_tag(pb, 8, 0x3002);
avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
} |
3f2795ed |
|
c6b579df |
// audio locked |
3f2795ed |
mxf_write_local_tag(pb, 1, 0x3D02); |
77eb5504 |
avio_w8(pb, 1); |
fda0d4af |
// write audio sampling rate
mxf_write_local_tag(pb, 8, 0x3D03); |
77eb5504 |
avio_wb32(pb, st->codec->sample_rate);
avio_wb32(pb, 1); |
fda0d4af |
mxf_write_local_tag(pb, 4, 0x3D07); |
3eae34d5 |
if (mxf->channel_count == -1) {
if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codec->channels != 4) && (st->codec->channels != 8)) |
134beb9e |
av_log(s, AV_LOG_WARNING, "the number of audio channels shall be 4 or 8 : the output will not comply to MXF D-10 specs, use -d10_channelcount to fix this\n"); |
3eae34d5 |
avio_wb32(pb, st->codec->channels);
} else if (s->oformat == &ff_mxf_d10_muxer) {
if (show_warnings && (mxf->channel_count < st->codec->channels)) |
134beb9e |
av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n"); |
3eae34d5 |
if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8)) |
134beb9e |
av_log(s, AV_LOG_WARNING, "d10_channelcount shall be set to 4 or 8 : the output will not comply to MXF D-10 specs\n"); |
3eae34d5 |
avio_wb32(pb, mxf->channel_count);
} else { |
415f1fab |
if (show_warnings && mxf->channel_count != -1 && s->oformat != &ff_mxf_opatom_muxer) |
134beb9e |
av_log(s, AV_LOG_ERROR, "-d10_channelcount requires MXF D-10 and will be ignored\n"); |
3eae34d5 |
avio_wb32(pb, st->codec->channels);
} |
fda0d4af |
mxf_write_local_tag(pb, 4, 0x3D01); |
77eb5504 |
avio_wb32(pb, av_get_bits_per_sample(st->codec->codec_id)); |
fda0d4af |
}
|
db6db7e5 |
static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) |
c6b579df |
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
f9303ffd |
|
42c6850e |
mxf_write_generic_sound_common(s, st, key, size+6+8); |
f9303ffd |
mxf_write_local_tag(pb, 2, 0x3D0A); |
77eb5504 |
avio_wb16(pb, st->codec->block_align); |
f9303ffd |
// avg bytes per sec
mxf_write_local_tag(pb, 4, 0x3D09); |
77eb5504 |
avio_wb32(pb, st->codec->block_align*st->codec->sample_rate); |
c6b579df |
}
|
0290074c |
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st) |
baed2ec4 |
{ |
42c6850e |
mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 0); |
0290074c |
} |
f9303ffd |
|
0290074c |
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
{ |
42c6850e |
mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 0); |
baed2ec4 |
}
|
bd3f81f3 |
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
{ |
42c6850e |
mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 0); |
bd3f81f3 |
}
|
8d54850f |
static const uint8_t mxf_indirect_value_utf16le[] = { 0x4c,0x00,0x02,0x10,0x01,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
{
MXFContext *mxf = s->priv_data;
AVIOContext *pb = s->pb;
int name_size = mxf_utf16_local_tag_length(name);
int indirect_value_size = 13 + mxf_utf16_local_tag_length(value);
if (!name_size || indirect_value_size == 13)
return 1;
mxf_write_metadata_key(pb, 0x013f00);
klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
// write instance UID
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count);
// write name
mxf_write_local_tag_utf16(pb, 0x5001, name); // Name
// write indirect value
mxf_write_local_tag(pb, indirect_value_size, 0x5003);
avio_write(pb, mxf_indirect_value_utf16le, 17);
avio_put_str16le(pb, value);
mxf->tagged_value_count++;
return 0;
}
static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
{
MXFContext *mxf = s->priv_data;
AVDictionaryEntry *t = NULL;
int count = 0;
while ((t = av_dict_get(m, "comment_", t, AV_DICT_IGNORE_SUFFIX))) {
if (mxf->tagged_value_count >= UINT16_MAX) {
av_log(s, AV_LOG_ERROR, "too many tagged values, ignoring remaining\n");
return count;
}
if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
count++;
}
return count;
}
|
399e3141 |
static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type, const char *package_name) |
98f7c5e0 |
{ |
f5b28890 |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
659713bf |
int i, track_count = s->nb_streams+1; |
399e3141 |
int name_size = mxf_utf16_local_tag_length(package_name); |
8d54850f |
int user_comment_count = 0; |
98f7c5e0 |
if (type == MaterialPackage) { |
9a6d5810 |
if (mxf->store_user_comments)
user_comment_count = mxf_write_user_comments(s, s->metadata); |
98f7c5e0 |
mxf_write_metadata_key(pb, 0x013600);
PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16); |
9a6d5810 |
klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12*mxf->store_user_comments); |
98f7c5e0 |
} else {
mxf_write_metadata_key(pb, 0x013700);
PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16); |
9a6d5810 |
klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12*mxf->store_user_comments); // 20 bytes length for descriptor reference |
98f7c5e0 |
}
// write uid
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, type, 0);
av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
// write package umid
mxf_write_local_tag(pb, 32, 0x4401); |
cafdc187 |
mxf_write_umid(s, type == SourcePackage); |
98f7c5e0 |
PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
|
399e3141 |
// package name
if (name_size)
mxf_write_local_tag_utf16(pb, 0x4402, package_name);
|
f5b28890 |
// package creation date |
98f7c5e0 |
mxf_write_local_tag(pb, 8, 0x4405); |
77eb5504 |
avio_wb64(pb, mxf->timestamp); |
98f7c5e0 |
|
f5b28890 |
// package modified date |
98f7c5e0 |
mxf_write_local_tag(pb, 8, 0x4404); |
77eb5504 |
avio_wb64(pb, mxf->timestamp); |
98f7c5e0 |
// write track refs |
7c9668cf |
mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
mxf_write_refs_count(pb, track_count); |
659713bf |
mxf_write_uuid(pb, type == MaterialPackage ? Track :
Track + TypeBottom, -1); // timecode track |
98f7c5e0 |
for (i = 0; i < s->nb_streams; i++)
mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
|
8d54850f |
// write user comment refs |
9a6d5810 |
if (mxf->store_user_comments) {
mxf_write_local_tag(pb, user_comment_count*16 + 8, 0x4406);
mxf_write_refs_count(pb, user_comment_count);
for (i = 0; i < user_comment_count; i++)
mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);
} |
8d54850f |
|
98f7c5e0 |
// write multiple descriptor reference
if (type == SourcePackage) {
mxf_write_local_tag(pb, 16, 0x4701); |
b1878364 |
if (s->nb_streams > 1) {
mxf_write_uuid(pb, MultipleDescriptor, 0);
mxf_write_multi_descriptor(s);
} else
mxf_write_uuid(pb, SubDescriptor, 0); |
6ae9d3fa |
} |
fda0d4af |
|
659713bf |
// write timecode track
mxf_write_track(s, mxf->timecode_track, type);
mxf_write_sequence(s, mxf->timecode_track, type);
mxf_write_timecode_component(s, mxf->timecode_track, type);
|
38573e8f |
for (i = 0; i < s->nb_streams; i++) { |
064abb8a |
AVStream *st = s->streams[i];
mxf_write_track(s, st, type);
mxf_write_sequence(s, st, type);
mxf_write_structural_component(s, st, type); |
fda0d4af |
if (type == SourcePackage) { |
f2ebd159 |
MXFStreamContext *sc = st->priv_data; |
d4ffc57d |
mxf_essence_container_uls[sc->index].write_desc(s, st); |
fda0d4af |
}
}
}
|
6e4668b8 |
static int mxf_write_essence_container_data(AVFormatContext *s)
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
6e4668b8 |
mxf_write_metadata_key(pb, 0x012300); |
0e9dbd96 |
klv_encode_ber_length(pb, 72); |
6e4668b8 |
mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
mxf_write_uuid(pb, EssenceContainerData, 0);
mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID |
cafdc187 |
mxf_write_umid(s, 1); |
6e4668b8 |
mxf_write_local_tag(pb, 4, 0x3F07); // BodySID |
77eb5504 |
avio_wb32(pb, 1); |
6e4668b8 |
|
0e9dbd96 |
mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID |
77eb5504 |
avio_wb32(pb, 2); |
0e9dbd96 |
|
6e4668b8 |
return 0;
}
|
2c8d4388 |
static int mxf_write_header_metadata_sets(AVFormatContext *s)
{ |
399e3141 |
const char *material_package_name = NULL;
const char *file_package_name = NULL;
AVDictionaryEntry *entry = NULL;
AVStream *st = NULL;
int i;
if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
material_package_name = entry->value;
if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
file_package_name = entry->value;
} else {
/* check if any of the streams contain a file_package_name */
for (i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
file_package_name = entry->value;
break;
}
}
}
|
a8f6e362 |
mxf_write_preface(s); |
9b9466b0 |
mxf_write_identification(s);
mxf_write_content_storage(s); |
399e3141 |
mxf_write_package(s, MaterialPackage, material_package_name);
mxf_write_package(s, SourcePackage, file_package_name); |
6e4668b8 |
mxf_write_essence_container_data(s); |
2c8d4388 |
return 0;
}
|
d526105f |
static unsigned klv_fill_size(uint64_t size) |
85d0d114 |
{ |
d526105f |
unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1)); |
baaf7b1b |
if (pad < 20) // smallest fill item possible |
85d0d114 |
return pad + KAG_SIZE;
else
return pad & (KAG_SIZE-1);
}
|
59453725 |
static void mxf_write_index_table_segment(AVFormatContext *s) |
0e9dbd96 |
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
29054fdd |
int i, j, temporal_reordering = 0; |
0cfa3596 |
int key_index = mxf->last_key_index; |
0e9dbd96 |
av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
|
bd3f81f3 |
if (!mxf->edit_units_count && !mxf->edit_unit_byte_count) |
99b170f8 |
return;
|
77eb5504 |
avio_write(pb, index_table_segment_key, 16); |
bd3f81f3 |
if (mxf->edit_unit_byte_count) { |
c3ff541b |
klv_encode_ber_length(pb, 80); |
bd3f81f3 |
} else { |
e3ba817b |
klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1LL)*6 +
12+mxf->edit_units_count*(11+mxf->slice_count*4LL)); |
bd3f81f3 |
} |
0e9dbd96 |
// instance id
mxf_write_local_tag(pb, 16, 0x3C0A);
mxf_write_uuid(pb, IndexTableSegment, 0);
// index edit rate
mxf_write_local_tag(pb, 8, 0x3F0B); |
77eb5504 |
avio_wb32(pb, mxf->time_base.den);
avio_wb32(pb, mxf->time_base.num); |
0e9dbd96 |
// index start position
mxf_write_local_tag(pb, 8, 0x3F0C); |
77eb5504 |
avio_wb64(pb, mxf->last_indexed_edit_unit); |
0e9dbd96 |
// index duration
mxf_write_local_tag(pb, 8, 0x3F0D); |
df1550f0 |
if (mxf->edit_unit_byte_count) |
77eb5504 |
avio_wb64(pb, 0); // index table covers whole container |
df1550f0 |
else |
77eb5504 |
avio_wb64(pb, mxf->edit_units_count); |
0e9dbd96 |
// edit unit byte count
mxf_write_local_tag(pb, 4, 0x3F05); |
77eb5504 |
avio_wb32(pb, mxf->edit_unit_byte_count); |
0e9dbd96 |
// index sid
mxf_write_local_tag(pb, 4, 0x3F06); |
77eb5504 |
avio_wb32(pb, 2); |
0e9dbd96 |
// body sid
mxf_write_local_tag(pb, 4, 0x3F07); |
77eb5504 |
avio_wb32(pb, 1); |
0e9dbd96 |
|
bd3f81f3 |
if (!mxf->edit_unit_byte_count) { |
c3ff541b |
// real slice count - 1
mxf_write_local_tag(pb, 1, 0x3F08); |
77eb5504 |
avio_w8(pb, mxf->slice_count); |
c3ff541b |
|
8d1a5af7 |
// delta entry array
mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09); |
77eb5504 |
avio_wb32(pb, s->nb_streams+1); // num of entries
avio_wb32(pb, 6); // size of one entry |
8d1a5af7 |
// write system item delta entry |
77eb5504 |
avio_w8(pb, 0);
avio_w8(pb, 0); // slice entry
avio_wb32(pb, 0); // element delta |
8d1a5af7 |
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i];
MXFStreamContext *sc = st->priv_data; |
77eb5504 |
avio_w8(pb, sc->temporal_reordering); |
8d1a5af7 |
if (sc->temporal_reordering)
temporal_reordering = 1;
if (i == 0) { // video track |
77eb5504 |
avio_w8(pb, 0); // slice number
avio_wb32(pb, KAG_SIZE); // system item size including klv fill |
8d1a5af7 |
} else { // audio track
unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
audio_frame_size += klv_fill_size(audio_frame_size); |
77eb5504 |
avio_w8(pb, 1);
avio_wb32(pb, (i-1)*audio_frame_size); // element delta |
8d1a5af7 |
} |
d526105f |
} |
0e9dbd96 |
|
8d1a5af7 |
mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A); |
77eb5504 |
avio_wb32(pb, mxf->edit_units_count); // num of entries
avio_wb32(pb, 11+mxf->slice_count*4); // size of one entry |
29054fdd |
|
8d1a5af7 |
for (i = 0; i < mxf->edit_units_count; i++) { |
61f9e3c1 |
int temporal_offset = 0; |
29054fdd |
if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
mxf->last_key_index = key_index;
key_index = i;
}
|
8d1a5af7 |
if (temporal_reordering) { |
29054fdd |
int pic_num_in_gop = i - key_index;
if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
for (j = key_index; j < mxf->edit_units_count; j++) {
if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
break; |
a4397fa9 |
} |
29054fdd |
if (j == mxf->edit_units_count)
av_log(s, AV_LOG_WARNING, "missing frames\n");
temporal_offset = j - key_index - pic_num_in_gop; |
0e9dbd96 |
} |
61f9e3c1 |
} |
77eb5504 |
avio_w8(pb, temporal_offset); |
61f9e3c1 |
if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction |
77eb5504 |
avio_w8(pb, mxf->last_key_index - i); |
8d1a5af7 |
} else { |
77eb5504 |
avio_w8(pb, key_index - i); // key frame offset |
8d1a5af7 |
if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
mxf->last_key_index = key_index;
} |
29054fdd |
if (!(mxf->index_entries[i].flags & 0x33) && // I frame
mxf->index_entries[i].flags & 0x40 && !temporal_offset)
mxf->index_entries[i].flags |= 0x80; // random access |
77eb5504 |
avio_w8(pb, mxf->index_entries[i].flags); |
8d1a5af7 |
// stream offset |
77eb5504 |
avio_wb64(pb, mxf->index_entries[i].offset); |
8d1a5af7 |
if (s->nb_streams > 1) |
77eb5504 |
avio_wb32(pb, mxf->index_entries[i].slice_offset); |
1c7c6669 |
} |
99b170f8 |
|
8d1a5af7 |
mxf->last_key_index = key_index - mxf->edit_units_count;
mxf->last_indexed_edit_unit += mxf->edit_units_count;
mxf->edit_units_count = 0; |
bd3f81f3 |
} |
0e9dbd96 |
}
|
b25ee3fb |
static void mxf_write_klv_fill(AVFormatContext *s)
{ |
a2704c97 |
unsigned pad = klv_fill_size(avio_tell(s->pb)); |
b25ee3fb |
if (pad) { |
77eb5504 |
avio_write(s->pb, klv_fill_key, 16); |
baaf7b1b |
pad -= 16 + 4;
klv_encode_ber4_length(s->pb, pad); |
95126728 |
ffio_fill(s->pb, 0, pad); |
db03f91f |
av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1))); |
b25ee3fb |
}
}
|
7684a361 |
static int mxf_write_partition(AVFormatContext *s, int bodysid, |
7524f46e |
int indexsid, |
0e9dbd96 |
const uint8_t *key, int write_metadata) |
6986662b |
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
85b5ee2b |
int64_t header_byte_count_offset; |
7524f46e |
unsigned index_byte_count = 0; |
a2704c97 |
uint64_t partition_offset = avio_tell(pb); |
7684a361 |
int err; |
7524f46e |
|
bd3f81f3 |
if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
12+mxf->edit_units_count*(11+mxf->slice_count*4);
else if (mxf->edit_unit_byte_count && indexsid) |
7b05a816 |
index_byte_count = 80; |
bd3f81f3 |
if (index_byte_count) { |
7524f46e |
// add encoded ber length
index_byte_count += 16 + klv_ber_length(index_byte_count);
index_byte_count += klv_fill_size(index_byte_count); |
99b170f8 |
}
|
6a8a3bfb |
if (key && !memcmp(key, body_partition_key, 16)) { |
7684a361 |
if ((err = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,
sizeof(*mxf->body_partition_offset))) < 0) {
mxf->body_partitions_count = 0;
return err;
} |
9c91a0b0 |
mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset; |
7524f46e |
} |
a8f6e362 |
|
6986662b |
// write klv |
6a8a3bfb |
if (key)
avio_write(pb, key, 16);
else
avio_write(pb, body_partition_key, 16);
|
5c108092 |
klv_encode_ber_length(pb, 88 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count)); |
6986662b |
// write partition value |
77eb5504 |
avio_wb16(pb, 1); // majorVersion
avio_wb16(pb, 2); // minorVersion
avio_wb32(pb, KAG_SIZE); // KAGSize |
6986662b |
|
77eb5504 |
avio_wb64(pb, partition_offset); // ThisPartition |
99b170f8 |
|
6a8a3bfb |
if (key && !memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1) |
77eb5504 |
avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition |
6a8a3bfb |
else if (key && !memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count) |
77eb5504 |
avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition |
99b170f8 |
else |
77eb5504 |
avio_wb64(pb, 0); |
6986662b |
|
77eb5504 |
avio_wb64(pb, mxf->footer_partition_offset); // footerPartition |
6986662b |
// set offset |
a2704c97 |
header_byte_count_offset = avio_tell(pb); |
77eb5504 |
avio_wb64(pb, 0); // headerByteCount, update later |
6986662b |
|
0e9dbd96 |
// indexTable |
77eb5504 |
avio_wb64(pb, index_byte_count); // indexByteCount
avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID |
99b170f8 |
// BodyOffset |
6a8a3bfb |
if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && s->oformat != &ff_mxf_opatom_muxer) |
77eb5504 |
avio_wb64(pb, mxf->body_offset); |
6a8a3bfb |
else |
77eb5504 |
avio_wb64(pb, 0); |
6986662b |
|
77eb5504 |
avio_wb32(pb, bodysid); // bodySID |
216b6631 |
// operational pattern |
6a8a3bfb |
if (s->oformat == &ff_mxf_opatom_muxer)
avio_write(pb, opatom_ul, 16);
else
avio_write(pb, op1a_ul, 16); |
6986662b |
// essence container |
3eaa53b3 |
mxf_write_essence_container_refs(s); |
cc984060 |
if (write_metadata) {
// mark the start of the headermetadata and calculate metadata size |
7be34052 |
int64_t pos, start; |
85d0d114 |
unsigned header_byte_count;
|
b25ee3fb |
mxf_write_klv_fill(s); |
a2704c97 |
start = avio_tell(s->pb); |
cc984060 |
mxf_write_primer_pack(s);
mxf_write_header_metadata_sets(s); |
a2704c97 |
pos = avio_tell(s->pb); |
d526105f |
header_byte_count = pos - start + klv_fill_size(pos); |
85d0d114 |
|
cc984060 |
// update header_byte_count |
6b4aa5da |
avio_seek(pb, header_byte_count_offset, SEEK_SET); |
77eb5504 |
avio_wb64(pb, header_byte_count); |
6b4aa5da |
avio_seek(pb, pos, SEEK_SET); |
cc984060 |
}
|
6a8a3bfb |
if(key)
avio_flush(pb); |
7684a361 |
return 0; |
6986662b |
}
|
d99001ca |
static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st,
AVPacket *pkt)
{
MXFContext *mxf = s->priv_data;
MXFStreamContext *sc = st->priv_data;
int i, cid;
uint8_t* header_cid; |
8214c1d8 |
int frame_size = 0; |
d99001ca |
if (mxf->header_written)
return 1;
if (pkt->size < 43)
return -1;
header_cid = pkt->data + 0x28;
cid = header_cid[0] << 24 | header_cid[1] << 16 | header_cid[2] << 8 | header_cid[3];
|
8214c1d8 |
if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) < 0) |
d99001ca |
return -1; |
d90f91c1 |
if ((sc->interlaced = avpriv_dnxhd_get_interlaced(cid)) < 0)
return AVERROR_INVALIDDATA; |
d99001ca |
switch (cid) {
case 1235:
sc->index = 24;
sc->component_depth = 10;
break;
case 1237:
sc->index = 25;
break;
case 1238:
sc->index = 26;
break;
case 1241:
sc->index = 27;
sc->component_depth = 10;
break;
case 1242:
sc->index = 28;
break;
case 1243:
sc->index = 29;
break;
case 1250:
sc->index = 30;
sc->component_depth = 10;
break;
case 1251:
sc->index = 31;
break;
case 1252:
sc->index = 32;
break;
case 1253:
sc->index = 33;
break;
default:
return -1;
}
|
b8a90976 |
sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
sc->aspect_ratio = (AVRational){ 16, 9 }; |
d99001ca |
|
6a8a3bfb |
if(s->oformat == &ff_mxf_opatom_muxer){
mxf->edit_unit_byte_count = frame_size;
return 1;
}
|
d99001ca |
mxf->edit_unit_byte_count = KAG_SIZE;
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i];
MXFStreamContext *sc = st->priv_data;
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
} else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
mxf->edit_unit_byte_count += 16 + 4 + frame_size;
mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
}
}
return 1;
}
|
135e6843 |
static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
MXFContext *mxf = s->priv_data;
MXFStreamContext *sc = st->priv_data;
uint8_t *vs_pack, *vsc_pack;
int i, ul_index, frame_size, stype, pal;
if (mxf->header_written)
return 1;
// Check for minimal frame size
if (pkt->size < 120000)
return -1;
vs_pack = pkt->data + 80*5 + 48;
vsc_pack = pkt->data + 80*5 + 53;
stype = vs_pack[3] & 0x1f;
pal = (vs_pack[3] >> 5) & 0x1;
if ((vs_pack[2] & 0x07) == 0x02)
sc->aspect_ratio = (AVRational){ 16, 9 };
else
sc->aspect_ratio = (AVRational){ 4, 3 };
sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
// TODO: fix dv encoder to set proper FF/FS value in VSC pack
// and set field dominance accordingly
// av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
switch (stype) {
case 0x18: // DV100 720p
ul_index = 6 + pal;
frame_size = pal ? 288000 : 240000;
if (sc->interlaced) { |
08277a45 |
av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n"); |
135e6843 |
sc->interlaced = 0;
}
break;
case 0x14: // DV100 1080i
ul_index = 4 + pal;
frame_size = pal ? 576000 : 480000;
break;
case 0x04: // DV50
ul_index = 2 + pal;
frame_size = pal ? 288000 : 240000;
break;
default: // DV25
ul_index = 0 + pal;
frame_size = pal ? 144000 : 120000;
}
sc->index = ul_index + 16;
sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
|
6a8a3bfb |
if(s->oformat == &ff_mxf_opatom_muxer) {
mxf->edit_unit_byte_count = frame_size;
return 1;
}
|
135e6843 |
mxf->edit_unit_byte_count = KAG_SIZE;
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i];
MXFStreamContext *sc = st->priv_data;
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
} else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
mxf->edit_unit_byte_count += 16 + 4 + frame_size;
mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
}
}
return 1;
}
|
2114e884 |
static const struct {
UID uid;
int frame_size;
int profile;
uint8_t interlaced;
} mxf_h264_codec_uls[] = {
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 }, 0, 110, 0 }, // AVC High 10 Intra
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 232960, 0, 1 }, // AVC Intra 50 1080i60
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 281088, 0, 1 }, // AVC Intra 50 1080i50
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 232960, 0, 0 }, // AVC Intra 50 1080p30
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 281088, 0, 0 }, // AVC Intra 50 1080p25
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x08 }, 116736, 0, 0 }, // AVC Intra 50 720p60
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x09 }, 140800, 0, 0 }, // AVC Intra 50 720p50
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 }, 0, 122, 0 }, // AVC High 422 Intra
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x01 }, 472576, 0, 1 }, // AVC Intra 100 1080i60
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x02 }, 568832, 0, 1 }, // AVC Intra 100 1080i50
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x03 }, 472576, 0, 0 }, // AVC Intra 100 1080p30
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x04 }, 568832, 0, 0 }, // AVC Intra 100 1080p25
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x08 }, 236544, 0, 0 }, // AVC Intra 100 720p60
{{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x09 }, 284672, 0, 0 }, // AVC Intra 100 720p50
};
static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st,
AVPacket *pkt, MXFIndexEntry *e)
{
MXFContext *mxf = s->priv_data;
MXFStreamContext *sc = st->priv_data;
static const int mxf_h264_num_codec_uls = sizeof(mxf_h264_codec_uls) / sizeof(mxf_h264_codec_uls[0]);
const uint8_t *buf = pkt->data;
const uint8_t *buf_end = pkt->data + pkt->size;
uint32_t state = -1;
int extra_size = 512; // support AVC Intra files without SPS/PPS header
int i, frame_size;
uint8_t uid_found;
if (pkt->size > extra_size)
buf_end -= pkt->size - extra_size; // no need to parse beyond SPS/PPS header
for (;;) {
buf = avpriv_find_start_code(buf, buf_end, &state);
if (buf >= buf_end)
break;
--buf;
switch (state & 0x1f) {
case NAL_SPS:
st->codec->profile = buf[1];
e->flags |= 0x40;
break;
case NAL_PPS:
if (e->flags & 0x40) { // sequence header present
e->flags |= 0x80; // random access
extra_size = 0;
buf = buf_end;
}
break;
default:
break;
}
}
if (mxf->header_written)
return 1;
sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
sc->component_depth = 10; // AVC Intra is always 10 Bit
sc->interlaced = st->codec->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
if (sc->interlaced)
sc->field_dominance = 1; // top field first is mandatory for AVC Intra
uid_found = 0;
frame_size = pkt->size + extra_size;
for (i = 0; i < mxf_h264_num_codec_uls; i++) {
if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
sc->codec_ul = &mxf_h264_codec_uls[i].uid;
return 1;
} else if (st->codec->profile == mxf_h264_codec_uls[i].profile) {
sc->codec_ul = &mxf_h264_codec_uls[i].uid;
uid_found = 1;
}
}
if (!uid_found) {
av_log(s, AV_LOG_ERROR, "AVC Intra 50/100 supported only\n");
return 0;
}
return 1;
}
|
4d418883 |
static const UID mxf_mpeg2_codec_uls[] = {
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP |
4460b81f |
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP |
4d418883 |
};
static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
{ |
4eba9cdd |
int long_gop = avctx->gop_size > 1 || avctx->has_b_frames;
|
4d418883 |
if (avctx->profile == 4) { // Main
if (avctx->level == 8) // Main |
4eba9cdd |
return &mxf_mpeg2_codec_uls[0+long_gop]; |
4d418883 |
else if (avctx->level == 4) // High |
4eba9cdd |
return &mxf_mpeg2_codec_uls[4+long_gop]; |
4460b81f |
else if (avctx->level == 6) // High 14
return &mxf_mpeg2_codec_uls[8+long_gop]; |
4d418883 |
} else if (avctx->profile == 0) { // 422
if (avctx->level == 5) // Main |
4eba9cdd |
return &mxf_mpeg2_codec_uls[2+long_gop]; |
4d418883 |
else if (avctx->level == 2) // High |
4eba9cdd |
return &mxf_mpeg2_codec_uls[6+long_gop]; |
4d418883 |
}
return NULL;
}
|
29054fdd |
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st,
AVPacket *pkt, MXFIndexEntry *e) |
7c7c1a76 |
{
MXFStreamContext *sc = st->priv_data;
uint32_t c = -1;
int i;
for(i = 0; i < pkt->size - 4; i++) {
c = (c<<8) + pkt->data[i]; |
b399b932 |
if (c == 0x1b5) { |
58a83023 |
if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext |
7c7c1a76 |
st->codec->profile = pkt->data[i+1] & 0x07;
st->codec->level = pkt->data[i+2] >> 4; |
52e5cd2b |
} else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame |
f6d952f5 |
if (sc->interlaced)
sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first |
7c7c1a76 |
break;
} |
0e9dbd96 |
} else if (c == 0x1b8) { // gop |
61f9e3c1 |
if (pkt->data[i+4]>>6 & 0x01) { // closed
sc->closed_gop = 1; |
29054fdd |
if (e->flags & 0x40) // sequence header present
e->flags |= 0x80; // random access |
61f9e3c1 |
} |
0e9dbd96 |
} else if (c == 0x1b3) { // seq |
29054fdd |
e->flags |= 0x40; |
df099252 |
switch ((pkt->data[i+4]>>4) & 0xf) {
case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
case 4: sc->aspect_ratio = (AVRational){221,100}; break;
default:
av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
st->codec->width, st->codec->height, 1024*1024);
} |
0e9dbd96 |
} else if (c == 0x100) { // pic
int pict_type = (pkt->data[i+2]>>3) & 0x07; |
29054fdd |
e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6); |
0e9dbd96 |
if (pict_type == 2) { // P frame |
29054fdd |
e->flags |= 0x22; |
61f9e3c1 |
sc->closed_gop = 0; // reset closed gop, don't matter anymore |
0e9dbd96 |
} else if (pict_type == 3) { // B frame |
61f9e3c1 |
if (sc->closed_gop) |
29054fdd |
e->flags |= 0x13; // only backward prediction |
61f9e3c1 |
else |
29054fdd |
e->flags |= 0x33; |
0e9dbd96 |
sc->temporal_reordering = -1;
} else if (!pict_type) {
av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
return 0;
} |
7c7c1a76 |
}
} |
c6610a21 |
if (s->oformat != &ff_mxf_d10_muxer) |
bd3f81f3 |
sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec); |
7c7c1a76 |
return !!sc->codec_ul;
}
|
f5b28890 |
static uint64_t mxf_parse_timestamp(time_t timestamp)
{ |
82ee7d0d |
struct tm tmbuf;
struct tm *time = gmtime_r(×tamp, &tmbuf); |
6dc7dc08 |
if (!time)
return 0; |
f5b28890 |
return (uint64_t)(time->tm_year+1900) << 48 |
(uint64_t)(time->tm_mon+1) << 40 |
(uint64_t) time->tm_mday << 32 |
time->tm_hour << 24 |
time->tm_min << 16 |
time->tm_sec << 8;
}
|
cafdc187 |
static void mxf_gen_umid(AVFormatContext *s)
{
MXFContext *mxf = s->priv_data; |
576fb48e |
uint32_t seed = av_get_random_seed(); |
cafdc187 |
uint64_t umid = seed + 0x5294713400000000LL;
AV_WB64(mxf->umid , umid);
AV_WB64(mxf->umid+8, umid>>8);
|
a0fec71e |
mxf->instance_number = seed & 0xFFFFFF; |
cafdc187 |
}
|
415f1fab |
static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational rate)
{
MXFContext *mxf = s->priv_data;
AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
if (!tcr)
tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
if (tcr)
return av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s);
else
return av_timecode_init(&mxf->tc, rate, 0, 0, s);
}
|
4e2f346b |
static int mxf_write_header(AVFormatContext *s) |
2c8d4388 |
{
MXFContext *mxf = s->priv_data; |
bf6e83a8 |
int i, ret; |
37d3e066 |
uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0}; |
906a2638 |
const MXFSamplesPerFrame *spf = NULL; |
5f847bf6 |
AVDictionaryEntry *t;
int64_t timestamp = 0; |
17930876 |
|
cafdc187 |
if (!s->nb_streams)
return -1;
|
6a8a3bfb |
if (s->oformat == &ff_mxf_opatom_muxer && s->nb_streams !=1){
av_log(s, AV_LOG_ERROR, "there must be exactly one stream for mxf opatom\n");
return -1;
}
|
9a6d5810 |
if (!av_dict_get(s->metadata, "comment_", NULL, AV_DICT_IGNORE_SUFFIX))
mxf->store_user_comments = 0;
|
17930876 |
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i]; |
e14d374e |
MXFStreamContext *sc = av_mallocz(sizeof(*sc)); |
17930876 |
if (!sc)
return AVERROR(ENOMEM);
st->priv_data = sc; |
cc812c8c |
|
415f1fab |
if (((i == 0) ^ (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)) && s->oformat != &ff_mxf_opatom_muxer) { |
24e90d0c |
av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
return -1;
}
|
72415b2a |
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
58afb312 |
const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(st->codec->pix_fmt); |
194be1f4 |
// TODO: should be avg_frame_rate |
ac293b66 |
AVRational rate, tbc = st->time_base; |
d99001ca |
// Default component depth to 8
sc->component_depth = 8; |
e32d832a |
sc->h_chroma_sub_sample = 2; |
3b176af0 |
sc->color_siting = 0xFF; |
58afb312 |
if (pix_desc) { |
e32d832a |
sc->component_depth = pix_desc->comp[0].depth_minus1 + 1;
sc->h_chroma_sub_sample = 1 << pix_desc->log2_chroma_w; |
58afb312 |
} |
3b176af0 |
switch (ff_choose_chroma_location(s, st)) {
case AVCHROMA_LOC_TOPLEFT: sc->color_siting = 0; break;
case AVCHROMA_LOC_LEFT: sc->color_siting = 6; break;
case AVCHROMA_LOC_TOP: sc->color_siting = 1; break;
case AVCHROMA_LOC_CENTER: sc->color_siting = 3; break;
} |
58afb312 |
|
aad737c6 |
mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num; |
41f7e064 |
spf = ff_mxf_get_samples_per_frame(s, tbc); |
906a2638 |
if (!spf) { |
6b3484dc |
av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n",
tbc.den, tbc.num);
return AVERROR(EINVAL); |
cc812c8c |
} |
906a2638 |
mxf->time_base = spf->time_base; |
16dc5f20 |
rate = av_inv_q(mxf->time_base); |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den); |
415f1fab |
if((ret = mxf_init_timecode(s, st, rate)) < 0) |
bf6e83a8 |
return ret; |
415f1fab |
|
d73565d5 |
sc->video_bit_rate = st->codec->bit_rate ? st->codec->bit_rate : st->codec->rc_max_rate; |
c6610a21 |
if (s->oformat == &ff_mxf_d10_muxer) { |
d7a76255 |
if ((sc->video_bit_rate == 50000000) && (mxf->time_base.den == 25)) {
sc->index = 3;
} else if ((sc->video_bit_rate == 49999840 || sc->video_bit_rate == 50000000) && (mxf->time_base.den != 25)) {
sc->index = 5; |
52cf08b4 |
} else if (sc->video_bit_rate == 40000000) { |
bd3f81f3 |
if (mxf->time_base.den == 25) sc->index = 7;
else sc->index = 9; |
52cf08b4 |
} else if (sc->video_bit_rate == 30000000) { |
bd3f81f3 |
if (mxf->time_base.den == 25) sc->index = 11;
else sc->index = 13; |
c2c06678 |
} else { |
bd3f81f3 |
av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
return -1;
}
mxf->edit_unit_byte_count = KAG_SIZE; // system element |
52cf08b4 |
mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)sc->video_bit_rate * |
bd3f81f3 |
mxf->time_base.num / (8*mxf->time_base.den);
mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); |
906a2638 |
mxf->edit_unit_byte_count += 16 + 4 + 4 + spf->samples_per_frame[0]*8*4; |
bd3f81f3 |
mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); |
5a3d2541 |
sc->signal_standard = 1; |
bd3f81f3 |
} |
7630cce4 |
if (mxf->signal_standard >= 0)
sc->signal_standard = mxf->signal_standard; |
72415b2a |
} else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
cc812c8c |
if (st->codec->sample_rate != 48000) {
av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
return -1;
} |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); |
c6610a21 |
if (s->oformat == &ff_mxf_d10_muxer) { |
bd3f81f3 |
if (st->index != 1) {
av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
return -1;
} |
36ef5369 |
if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE &&
st->codec->codec_id != AV_CODEC_ID_PCM_S24LE) { |
bd3f81f3 |
av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
}
sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1; |
415f1fab |
} else if (s->oformat == &ff_mxf_opatom_muxer) {
AVRational tbc = av_inv_q(mxf->audio_edit_rate);
if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE &&
st->codec->codec_id != AV_CODEC_ID_PCM_S24LE) {
av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
return AVERROR_PATCHWELCOME;
}
if (st->codec->channels != 1) {
av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
return AVERROR(EINVAL);
}
spf = ff_mxf_get_samples_per_frame(s, tbc);
if (!spf){
av_log(s, AV_LOG_ERROR, "Unsupported timecode frame rate %d/%d\n", tbc.den, tbc.num);
return AVERROR(EINVAL);
}
mxf->time_base = st->time_base;
if((ret = mxf_init_timecode(s, st, av_inv_q(spf->time_base))) < 0)
return ret;
mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
mxf->edit_unit_byte_count = (av_get_bits_per_sample(st->codec->codec_id) * st->codec->channels) >> 3;
sc->index = 2;
} else {
mxf->slice_count = 1;
} |
cc812c8c |
} |
cdedd878 |
|
bd3f81f3 |
if (!sc->index) { |
8d1a5af7 |
sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
if (sc->index == -1) {
av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
"codec not currently supported in container\n", i);
return -1;
} |
bd3f81f3 |
} |
4d418883 |
|
7c7c1a76 |
sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul; |
4d418883 |
|
d4ffc57d |
memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
sc->track_essence_element_key[15] = present[sc->index]; |
b8ad3f78 |
PRINT_KEY(s, "track essence element key", sc->track_essence_element_key); |
ef33facd |
if (!present[sc->index])
mxf->essence_container_count++;
present[sc->index]++; |
17930876 |
} |
9b9466b0 |
|
6a8a3bfb |
if (s->oformat == &ff_mxf_d10_muxer || s->oformat == &ff_mxf_opatom_muxer) { |
bd3f81f3 |
mxf->essence_container_count = 1;
}
|
0c1959b0 |
if (!(s->flags & AVFMT_FLAG_BITEXACT)) |
cafdc187 |
mxf_gen_umid(s);
|
cc812c8c |
for (i = 0; i < s->nb_streams; i++) {
MXFStreamContext *sc = s->streams[i]->priv_data;
// update element count
sc->track_essence_element_key[13] = present[sc->index]; |
135e6843 |
if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[15].element_ul, 13)) // DV
sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
else
sc->order = AV_RB32(sc->track_essence_element_key+12); |
cc812c8c |
}
|
001d668d |
if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
timestamp = ff_iso8601_to_unix_time(t->value); |
5f847bf6 |
if (timestamp)
mxf->timestamp = mxf_parse_timestamp(timestamp); |
7c9668cf |
mxf->duration = -1;
mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
if (!mxf->timecode_track)
return AVERROR(ENOMEM);
mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
if (!mxf->timecode_track->priv_data)
return AVERROR(ENOMEM); |
659713bf |
mxf->timecode_track->index = -1; |
f5b28890 |
|
906a2638 |
if (!spf) |
41f7e064 |
spf = ff_mxf_get_samples_per_frame(s, (AVRational){ 1, 25 }); |
59bcc93b |
|
906a2638 |
if (ff_audio_interleave_init(s, spf->samples_per_frame, mxf->time_base) < 0) |
cc812c8c |
return -1;
|
9b9466b0 |
return 0;
}
|
67d8ac4a |
static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
static const uint8_t system_metadata_package_set_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x43,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x02,0x01 };
static void mxf_write_system_item(AVFormatContext *s)
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
7c9668cf |
unsigned frame; |
67d8ac4a |
uint32_t time_code;
|
bf6e83a8 |
frame = mxf->last_indexed_edit_unit + mxf->edit_units_count; |
67d8ac4a |
// write system metadata pack |
77eb5504 |
avio_write(pb, system_metadata_pack_key, 16); |
e15421fe |
klv_encode_ber4_length(pb, 57); |
77eb5504 |
avio_w8(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
avio_w8(pb, 0x04); // content package rate
avio_w8(pb, 0x00); // content package type
avio_wb16(pb, 0x00); // channel handle |
3896cd11 |
avio_wb16(pb, (mxf->tc.start + frame) & 0xFFFF); // continuity count, supposed to overflow |
67d8ac4a |
if (mxf->essence_container_count > 1) |
77eb5504 |
avio_write(pb, multiple_desc_ul, 16); |
844d9e41 |
else {
MXFStreamContext *sc = s->streams[0]->priv_data; |
77eb5504 |
avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); |
844d9e41 |
} |
77eb5504 |
avio_w8(pb, 0);
avio_wb64(pb, 0);
avio_wb64(pb, 0); // creation date/time stamp |
67d8ac4a |
|
77eb5504 |
avio_w8(pb, 0x81); // SMPTE 12M time code |
bf6e83a8 |
time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame); |
77eb5504 |
avio_wb32(pb, time_code);
avio_wb32(pb, 0); // binary group data
avio_wb64(pb, 0); |
67d8ac4a |
// write system metadata package set |
77eb5504 |
avio_write(pb, system_metadata_package_set_key, 16); |
e15421fe |
klv_encode_ber4_length(pb, 35); |
77eb5504 |
avio_w8(pb, 0x83); // UMID
avio_wb16(pb, 0x20); |
cafdc187 |
mxf_write_umid(s, 1); |
67d8ac4a |
}
|
bd3f81f3 |
static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
52cf08b4 |
MXFStreamContext *sc = st->priv_data;
int packet_size = (uint64_t)sc->video_bit_rate*mxf->time_base.num / |
bd3f81f3 |
(8*mxf->time_base.den); // frame size
int pad;
packet_size += 16 + 4;
packet_size += klv_fill_size(packet_size);
klv_encode_ber4_length(pb, pkt->size); |
77eb5504 |
avio_write(pb, pkt->data, pkt->size); |
bd3f81f3 |
// ensure CBR muxing by padding to correct video frame size
pad = packet_size - pkt->size - 16 - 4;
if (pad > 20) { |
77eb5504 |
avio_write(s->pb, klv_fill_key, 16); |
bd3f81f3 |
pad -= 16 + 4;
klv_encode_ber4_length(s->pb, pad); |
95126728 |
ffio_fill(s->pb, 0, pad); |
db03f91f |
av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1))); |
bd3f81f3 |
} else {
av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n"); |
95126728 |
ffio_fill(s->pb, 0, pad); |
bd3f81f3 |
}
}
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
e38badbc |
int frame_size = pkt->size / st->codec->block_align; |
bd3f81f3 |
uint8_t *samples = pkt->data;
uint8_t *end = pkt->data + pkt->size;
int i;
klv_encode_ber4_length(pb, 4 + frame_size*4*8);
|
77eb5504 |
avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
avio_wl16(pb, frame_size);
avio_w8(pb, (1<<st->codec->channels)-1); |
bd3f81f3 |
while (samples < end) {
for (i = 0; i < st->codec->channels; i++) {
uint32_t sample; |
36ef5369 |
if (st->codec->codec_id == AV_CODEC_ID_PCM_S24LE) { |
6df1deda |
sample = AV_RL24(samples)<< 4; |
bd3f81f3 |
samples += 3;
} else { |
6df1deda |
sample = AV_RL16(samples)<<12; |
bd3f81f3 |
samples += 2;
} |
77eb5504 |
avio_wl32(pb, sample | i); |
bd3f81f3 |
}
for (; i < 8; i++) |
77eb5504 |
avio_wl32(pb, i); |
bd3f81f3 |
}
}
|
6a8a3bfb |
static int mxf_write_opatom_body_partition(AVFormatContext *s)
{
MXFContext *mxf = s->priv_data;
AVIOContext *pb = s->pb;
AVStream *st = s->streams[0];
MXFStreamContext *sc = st->priv_data;
const uint8_t *key = NULL;
int err;
if (!mxf->header_written)
key = body_partition_key;
if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
return err;
mxf_write_klv_fill(s);
avio_write(pb, sc->track_essence_element_key, 16);
klv_encode_ber9_length(pb, mxf->body_offset);
return 0;
}
static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
{
MXFContext *mxf = s->priv_data;
AVIOContext *pb = s->pb;
int err;
if (!mxf->header_written) {
if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
return err;
mxf_write_klv_fill(s);
if ((err = mxf_write_opatom_body_partition(s)) < 0)
return err;
mxf->header_written = 1;
}
if (!mxf->edit_unit_byte_count) {
mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
mxf->index_entries[mxf->edit_units_count].flags = ie->flags;
mxf->index_entries[mxf->edit_units_count].temporal_ref = ie->temporal_ref;
}
mxf->edit_units_count++;
avio_write(pb, pkt->data, pkt->size);
mxf->body_offset += pkt->size;
avio_flush(pb);
return 0;
}
|
4e2f346b |
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt) |
9b0d3e0e |
{ |
7c7c1a76 |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
9b0d3e0e |
AVStream *st = s->streams[pkt->stream_index];
MXFStreamContext *sc = st->priv_data; |
29054fdd |
MXFIndexEntry ie = {0}; |
7684a361 |
int err; |
9b0d3e0e |
|
d1fa518e |
if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) { |
7684a361 |
if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
+ EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
mxf->edit_units_count = 0; |
0e9dbd96 |
av_log(s, AV_LOG_ERROR, "could not allocate index entries\n"); |
7684a361 |
return err; |
0e9dbd96 |
}
}
|
36ef5369 |
if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
29054fdd |
if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) { |
0e9dbd96 |
av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
return -1; |
7c7c1a76 |
} |
7a72695c |
} else if (st->codec->codec_id == AV_CODEC_ID_DNXHD) { |
d99001ca |
if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
return -1;
} |
7a72695c |
} else if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) { |
135e6843 |
if (!mxf_parse_dv_frame(s, st, pkt)) {
av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
return -1;
} |
2114e884 |
} else if (st->codec->codec_id == AV_CODEC_ID_H264) {
if (!mxf_parse_h264_frame(s, st, pkt, &ie)) {
av_log(s, AV_LOG_ERROR, "could not get h264 profile\n");
return -1;
} |
0e9dbd96 |
}
|
6a8a3bfb |
if (s->oformat == &ff_mxf_opatom_muxer)
return mxf_write_opatom_packet(s, pkt, &ie);
|
0e9dbd96 |
if (!mxf->header_written) { |
d1fa518e |
if (mxf->edit_unit_byte_count) { |
7684a361 |
if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
return err; |
d1fa518e |
mxf_write_klv_fill(s);
mxf_write_index_table_segment(s);
} else { |
7684a361 |
if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
return err; |
d1fa518e |
} |
7c7c1a76 |
mxf->header_written = 1;
}
|
5ccc2316 |
if (st->index == 0) { |
d1fa518e |
if (!mxf->edit_unit_byte_count &&
(!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) && |
29054fdd |
!(ie.flags & 0x33)) { // I frame, Gop start |
99b170f8 |
mxf_write_klv_fill(s); |
7684a361 |
if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
return err; |
99b170f8 |
mxf_write_klv_fill(s);
mxf_write_index_table_segment(s);
}
|
67d8ac4a |
mxf_write_klv_fill(s); |
bf31d93a |
mxf_write_system_item(s); |
67d8ac4a |
|
d1fa518e |
if (!mxf->edit_unit_byte_count) {
mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset; |
29054fdd |
mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
mxf->index_entries[mxf->edit_units_count].temporal_ref = ie.temporal_ref; |
d1fa518e |
mxf->body_offset += KAG_SIZE; // size of system element
} |
0e9dbd96 |
mxf->edit_units_count++; |
d1fa518e |
} else if (!mxf->edit_unit_byte_count && st->index == 1) { |
73d0869a |
mxf->index_entries[mxf->edit_units_count-1].slice_offset =
mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset; |
0e9dbd96 |
}
|
bf31d93a |
mxf_write_klv_fill(s); |
77eb5504 |
avio_write(pb, sc->track_essence_element_key, 16); // write key |
c6610a21 |
if (s->oformat == &ff_mxf_d10_muxer) { |
72415b2a |
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) |
d1fa518e |
mxf_write_d10_video_packet(s, st, pkt);
else
mxf_write_d10_audio_packet(s, st, pkt);
} else {
klv_encode_ber4_length(pb, pkt->size); // write length |
77eb5504 |
avio_write(pb, pkt->data, pkt->size); |
d1fa518e |
mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
} |
9b0d3e0e |
|
b7f2fdde |
avio_flush(pb); |
73d0869a |
|
9b0d3e0e |
return 0;
}
|
0e9dbd96 |
static void mxf_write_random_index_pack(AVFormatContext *s)
{
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
a2704c97 |
uint64_t pos = avio_tell(pb); |
99b170f8 |
int i; |
0e9dbd96 |
|
77eb5504 |
avio_write(pb, random_index_pack_key, 16); |
e3ba817b |
klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count); |
0e9dbd96 |
|
6a8a3bfb |
if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) |
77eb5504 |
avio_wb32(pb, 1); // BodySID of header partition |
6c53bf63 |
else |
77eb5504 |
avio_wb32(pb, 0);
avio_wb64(pb, 0); // offset of header partition |
0e9dbd96 |
|
99b170f8 |
for (i = 0; i < mxf->body_partitions_count; i++) { |
77eb5504 |
avio_wb32(pb, 1); // BodySID
avio_wb64(pb, mxf->body_partition_offset[i]); |
99b170f8 |
}
|
77eb5504 |
avio_wb32(pb, 0); // BodySID of footer partition
avio_wb64(pb, mxf->footer_partition_offset); |
0e9dbd96 |
|
a2704c97 |
avio_wb32(pb, avio_tell(pb) - pos + 4); |
0e9dbd96 |
}
|
4e2f346b |
static int mxf_write_footer(AVFormatContext *s) |
9b9466b0 |
{ |
cc984060 |
MXFContext *mxf = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
39ddda12 |
int err = 0; |
85d0d114 |
|
b61cb61a |
if (!mxf->header_written ||
(s->oformat == &ff_mxf_opatom_muxer && !mxf->body_partition_offset)) {
/* reason could be invalid options/not supported codec/out of memory */
err = AVERROR_UNKNOWN;
goto end;
}
|
7c9668cf |
mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
|
85d0d114 |
mxf_write_klv_fill(s); |
a2704c97 |
mxf->footer_partition_offset = avio_tell(pb); |
6a8a3bfb |
if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { // no need to repeat index |
7684a361 |
if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0) |
39ddda12 |
goto end; |
bd3f81f3 |
} else { |
7684a361 |
if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0) |
39ddda12 |
goto end; |
8d1a5af7 |
mxf_write_klv_fill(s);
mxf_write_index_table_segment(s); |
bd3f81f3 |
} |
0e9dbd96 |
|
b25ee3fb |
mxf_write_klv_fill(s); |
0e9dbd96 |
mxf_write_random_index_pack(s);
|
8978feda |
if (s->pb->seekable) { |
6a8a3bfb |
if (s->oformat == &ff_mxf_opatom_muxer){
/* rewrite body partition to update lengths */
avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
if ((err = mxf_write_opatom_body_partition(s)) < 0) |
39ddda12 |
goto end; |
6a8a3bfb |
}
|
6b4aa5da |
avio_seek(pb, 0, SEEK_SET); |
6a8a3bfb |
if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { |
7684a361 |
if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0) |
39ddda12 |
goto end; |
bd3f81f3 |
mxf_write_klv_fill(s);
mxf_write_index_table_segment(s); |
d1fa518e |
} else { |
7684a361 |
if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0) |
39ddda12 |
goto end; |
bd3f81f3 |
} |
cc984060 |
} |
06466638 |
|
39ddda12 |
end: |
06466638 |
ff_audio_interleave_close(s);
|
84a2d304 |
av_freep(&mxf->index_entries); |
99b170f8 |
av_freep(&mxf->body_partition_offset); |
7c9668cf |
av_freep(&mxf->timecode_track->priv_data);
av_freep(&mxf->timecode_track); |
84a2d304 |
|
9b9466b0 |
mxf_free(s); |
8d1a5af7 |
|
39ddda12 |
return err < 0 ? err : 0; |
2c8d4388 |
} |
9b9466b0 |
|
8f819636 |
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) |
cc812c8c |
{ |
012dc2c4 |
int i, stream_count = 0;
for (i = 0; i < s->nb_streams; i++)
stream_count += !!s->streams[i]->last_in_packet_buffer; |
cc812c8c |
if (stream_count && (s->nb_streams == stream_count || flush)) { |
9deaec78 |
AVPacketList *pktl = s->internal->packet_buffer; |
bc185fc1 |
if (s->nb_streams != stream_count) { |
6f0b1866 |
AVPacketList *last = NULL; |
fe5a361c |
// find last packet in edit unit |
bc185fc1 |
while (pktl) { |
fe5a361c |
if (!stream_count || pktl->pkt.stream_index == 0) |
bc185fc1 |
break; |
6f0b1866 |
last = pktl; |
bc185fc1 |
pktl = pktl->next; |
fe5a361c |
stream_count--; |
bc185fc1 |
} |
cc812c8c |
// purge packet queue
while (pktl) {
AVPacketList *next = pktl->next; |
e07b882b |
if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL; |
cc812c8c |
av_free_packet(&pktl->pkt);
av_freep(&pktl);
pktl = next;
} |
fe5a361c |
if (last)
last->next = NULL;
else { |
9deaec78 |
s->internal->packet_buffer = NULL;
s->internal->packet_buffer_end= NULL; |
bc185fc1 |
goto out; |
fe5a361c |
} |
9deaec78 |
pktl = s->internal->packet_buffer; |
cc812c8c |
}
|
bc185fc1 |
*out = pktl->pkt; |
1a3eb042 |
av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts); |
9deaec78 |
s->internal->packet_buffer = pktl->next; |
e07b882b |
if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL; |
9deaec78 |
if(!s->internal->packet_buffer)
s->internal->packet_buffer_end= NULL; |
bc185fc1 |
av_freep(&pktl); |
cc812c8c |
return 1;
} else { |
bc185fc1 |
out: |
cc812c8c |
av_init_packet(out);
return 0;
}
}
static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
{ |
5a897cfa |
MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data; |
cc812c8c |
|
5a897cfa |
return next->dts > pkt->dts ||
(next->dts == pkt->dts && sc->order < sc2->order); |
cc812c8c |
}
static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
{ |
16e3b0b3 |
return ff_audio_rechunk_interleave(s, out, pkt, flush, |
f1544e79 |
mxf_interleave_get_packet, mxf_compare_timestamps); |
cc812c8c |
}
|
7630cce4 |
#define MXF_COMMON_OPTIONS \
{ "signal_standard", "Force/set Sigal Standard",\
offsetof(MXFContext, signal_standard), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
{ "bt601", "ITU-R BT.601 and BT.656, also SMPTE 125M (525 and 625 line interlaced)",\
0, AV_OPT_TYPE_CONST, {.i64 = 1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
{ "bt1358", "ITU-R BT.1358 and ITU-R BT.799-3, also SMPTE 293M (525 and 625 line progressive)",\
0, AV_OPT_TYPE_CONST, {.i64 = 2}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
{ "smpte347m", "SMPTE 347M (540 Mbps mappings)",\
0, AV_OPT_TYPE_CONST, {.i64 = 3}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
{ "smpte274m", "SMPTE 274M (1125 line)",\
0, AV_OPT_TYPE_CONST, {.i64 = 4}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
{ "smpte296m", "SMPTE 296M (750 line progressive)",\
0, AV_OPT_TYPE_CONST, {.i64 = 5}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
{ "smpte349m", "SMPTE 349M (1485 Mbps mappings)",\
0, AV_OPT_TYPE_CONST, {.i64 = 6}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
{ "smpte428", "SMPTE 428-1 DCDM",\
0, AV_OPT_TYPE_CONST, {.i64 = 7}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},
|
f0737640 |
static const AVOption mxf_options[] = { |
7630cce4 |
MXF_COMMON_OPTIONS |
9a6d5810 |
{ "store_user_comments", "",
offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, |
f0737640 |
{ NULL },
};
static const AVClass mxf_muxer_class = {
.class_name = "MXF muxer",
.item_name = av_default_item_name,
.option = mxf_options,
.version = LIBAVUTIL_VERSION_INT,
};
|
3eae34d5 |
static const AVOption d10_options[] = { |
134beb9e |
{ "d10_channelcount", "Force/set channelcount in generic sound essence descriptor", |
3eae34d5 |
offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM}, |
7630cce4 |
MXF_COMMON_OPTIONS |
9a6d5810 |
{ "store_user_comments", "",
offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, |
3eae34d5 |
{ NULL },
};
static const AVClass mxf_d10_muxer_class = {
.class_name = "MXF-D10 muxer",
.item_name = av_default_item_name,
.option = d10_options,
.version = LIBAVUTIL_VERSION_INT,
};
|
415f1fab |
static const AVOption opatom_options[] = {
{ "mxf_audio_edit_rate", "Audio edit rate for timecode",
offsetof(MXFContext, audio_edit_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, |
7630cce4 |
MXF_COMMON_OPTIONS |
415f1fab |
{ NULL },
};
static const AVClass mxf_opatom_muxer_class = {
.class_name = "MXF-OPAtom muxer",
.item_name = av_default_item_name,
.option = opatom_options,
.version = LIBAVUTIL_VERSION_INT,
};
|
c6610a21 |
AVOutputFormat ff_mxf_muxer = { |
dfc2c4d9 |
.name = "mxf", |
0177b7d2 |
.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"), |
dfc2c4d9 |
.mime_type = "application/mxf",
.extensions = "mxf",
.priv_data_size = sizeof(MXFContext), |
36ef5369 |
.audio_codec = AV_CODEC_ID_PCM_S16LE,
.video_codec = AV_CODEC_ID_MPEG2VIDEO, |
dfc2c4d9 |
.write_header = mxf_write_header,
.write_packet = mxf_write_packet,
.write_trailer = mxf_write_footer,
.flags = AVFMT_NOTIMESTAMPS,
.interleave_packet = mxf_interleave, |
f0737640 |
.priv_class = &mxf_muxer_class, |
b5f46e93 |
}; |
bd3f81f3 |
|
c6610a21 |
AVOutputFormat ff_mxf_d10_muxer = { |
dfc2c4d9 |
.name = "mxf_d10", |
0177b7d2 |
.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"), |
dfc2c4d9 |
.mime_type = "application/mxf",
.priv_data_size = sizeof(MXFContext), |
36ef5369 |
.audio_codec = AV_CODEC_ID_PCM_S16LE,
.video_codec = AV_CODEC_ID_MPEG2VIDEO, |
dfc2c4d9 |
.write_header = mxf_write_header,
.write_packet = mxf_write_packet,
.write_trailer = mxf_write_footer,
.flags = AVFMT_NOTIMESTAMPS,
.interleave_packet = mxf_interleave, |
3eae34d5 |
.priv_class = &mxf_d10_muxer_class, |
bd3f81f3 |
}; |
6a8a3bfb |
AVOutputFormat ff_mxf_opatom_muxer = {
.name = "mxf_opatom",
.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) Operational Pattern Atom"),
.mime_type = "application/mxf",
.extensions = "mxf",
.priv_data_size = sizeof(MXFContext),
.audio_codec = AV_CODEC_ID_PCM_S16LE,
.video_codec = AV_CODEC_ID_DNXHD,
.write_header = mxf_write_header,
.write_packet = mxf_write_packet,
.write_trailer = mxf_write_footer,
.flags = AVFMT_NOTIMESTAMPS,
.interleave_packet = mxf_interleave, |
415f1fab |
.priv_class = &mxf_opatom_muxer_class, |
6a8a3bfb |
}; |