annotate gxfenc.c @ 1183:5b04d98e11bb libavformat

GXF muxer
author bcoudurier
date Wed, 19 Jul 2006 09:38:22 +0000
parents
children b4f48eb1f411
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1183
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
1 /*
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
2 * GXF muxer.
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
4 *
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or modify
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
8 * (at your option) any later version.
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
9 *
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
13 * GNU General Public License for more details.
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
14 *
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
18 */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
19
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
20 #include "avformat.h"
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
21 #include "gxf.h"
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
22 #include "riff.h"
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
23
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
24 #define GXF_AUDIO_PACKET_SIZE 65536
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
25
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
26 typedef struct GXFStreamContext {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
27 AVCodecContext *codec;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
28 FifoBuffer audio_buffer;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
29 uint32_t track_type;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
30 uint32_t sample_size;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
31 uint32_t sample_rate;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
32 uint16_t media_type;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
33 uint16_t media_info;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
34 uint8_t index;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
35 int frame_rate_index;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
36 int lines_index;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
37 int fields;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
38 int iframes;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
39 int pframes;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
40 int bframes;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
41 int p_per_gop;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
42 int b_per_gop;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
43 } GXFStreamContext;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
44
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
45 typedef struct GXFContext {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
46 uint32_t nb_frames;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
47 uint32_t material_flags;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
48 uint16_t audio_tracks;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
49 uint16_t mpeg_tracks;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
50 int64_t creation_time;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
51 uint32_t umf_start_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
52 uint32_t umf_track_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
53 uint32_t umf_media_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
54 uint32_t umf_user_data_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
55 uint32_t umf_user_data_size;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
56 uint32_t umf_length;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
57 uint16_t umf_track_size;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
58 uint16_t umf_media_size;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
59 int audio_written;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
60 int sample_rate;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
61 int field_number;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
62 int flags;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
63 AVFormatContext *fc;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
64 GXFStreamContext streams[48];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
65 } GXFContext;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
66
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
67 typedef struct GXF_Lines {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
68 int height;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
69 int index;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
70 } GXF_Lines;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
71
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
72
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
73 /* FIXME check if it is relevant */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
74 static const GXF_Lines gxf_lines_tab[] = {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
75 { 480, 1 }, /* NTSC */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
76 { 512, 1 }, /* NTSC + VBI */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
77 { 576, 2 }, /* PAL */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
78 { 608, 2 }, /* PAL + VBI */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
79 { 1080, 4 },
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
80 { 720, 6 },
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
81 };
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
82
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
83 static const CodecTag gxf_media_types[] = {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
84 { CODEC_ID_MJPEG , 3 }, /* NTSC */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
85 { CODEC_ID_MJPEG , 4 }, /* PAL */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
86 { CODEC_ID_PCM_S24LE , 9 },
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
87 { CODEC_ID_PCM_S16LE , 10 },
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
88 { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
89 { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
90 { CODEC_ID_DVVIDEO , 13 }, /* NTSC */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
91 { CODEC_ID_DVVIDEO , 14 }, /* PAL */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
92 { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
93 { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
94 { CODEC_ID_AC3 , 17 },
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
95 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
96 { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
97 { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
98 { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
99 { 0, 0 },
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
100 };
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
101
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
102 #define SERVER_PATH "/space/"
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
103 #define ES_NAME_PATTERN "ES."
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
104
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
105 static int gxf_find_lines_index(GXFStreamContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
106 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
107 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
108
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
109 for (i = 0; i < 6; ++i) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
110 if (ctx->codec->height == gxf_lines_tab[i].height) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
111 ctx->lines_index = gxf_lines_tab[i].index;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
112 return 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
113 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
114 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
115 return -1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
116 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
117
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
118 static void gxf_write_padding(ByteIOContext *pb, offset_t to_pad)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
119 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
120 while (to_pad--) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
121 put_byte(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
122 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
123 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
124
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
125 static offset_t updatePacketSize(ByteIOContext *pb, offset_t pos)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
126 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
127 offset_t curpos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
128 int size;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
129
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
130 size = url_ftell(pb) - pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
131 if (size % 4) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
132 gxf_write_padding(pb, 4 - size % 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
133 size = url_ftell(pb) - pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
134 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
135 curpos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
136 url_fseek(pb, pos + 6, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
137 put_be32(pb, size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
138 url_fseek(pb, curpos, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
139 return curpos - pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
140 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
141
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
142 static offset_t updateSize(ByteIOContext *pb, offset_t pos)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
143 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
144 offset_t curpos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
145
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
146 curpos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
147 url_fseek(pb, pos, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
148 put_be16(pb, curpos - pos - 2);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
149 url_fseek(pb, curpos, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
150 return curpos - pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
151 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
152
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
153 static void gxf_write_packet_header(ByteIOContext *pb, pkt_type_t type)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
154 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
155 put_be32(pb, 0); /* packet leader for synchro */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
156 put_byte(pb, 1);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
157 put_byte(pb, type); /* map packet */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
158 put_be32(pb, 0); /* size */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
159 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
160 put_byte(pb, 0xE1); /* trailer 1 */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
161 put_byte(pb, 0xE2); /* trailer 2 */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
162 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
163
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
164 static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
165 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
166 char buffer[1024];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
167 int size;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
168
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
169 if (ctx->iframes) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
170 ctx->p_per_gop = ctx->pframes / ctx->iframes;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
171 if (ctx->pframes % ctx->iframes)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
172 ctx->p_per_gop++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
173 if (ctx->pframes)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
174 ctx->b_per_gop = ctx->bframes / ctx->pframes;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
175 if (ctx->p_per_gop > 9)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
176 ctx->p_per_gop = 9; /* ensure value won't take more than one char */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
177 if (ctx->b_per_gop > 9)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
178 ctx->b_per_gop = 9; /* ensure value won't take more than one char */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
179 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
180 size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
181 "Pix 0\nCf %d\nCg 1\nSl 7\nnl16 %d\nVi 1\nf1 1\n",
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
182 (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
183 ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
184 ctx->codec->height / 16);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
185 put_byte(pb, 0x4F);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
186 put_byte(pb, size + 1);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
187 put_buffer(pb, (uint8_t *)buffer, size + 1);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
188 return size + 3;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
189 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
190
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
191 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
192 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
193 /* FIXME implement that */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
194 put_byte(pb, 0); /* fields */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
195 put_byte(pb, 0); /* seconds */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
196 put_byte(pb, 0); /* minutes */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
197 put_byte(pb, 0); /* flags + hours */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
198 /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
199 put_be32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
200 return 8;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
201 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
202
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
203 static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stream)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
204 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
205 offset_t pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
206
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
207 /* track description section */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
208 put_byte(pb, stream->media_type + 0x80);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
209 put_byte(pb, stream->index + 0xC0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
210
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
211 pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
212 put_be16(pb, 0); /* size */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
213
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
214 /* media file name */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
215 put_byte(pb, 0x4C);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
216 put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
217 put_tag(pb, ES_NAME_PATTERN);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
218 put_be16(pb, stream->media_info);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
219 put_byte(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
220
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
221 if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
222 /* auxiliary information */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
223 put_byte(pb, 0x4D);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
224 put_byte(pb, 8);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
225 if (stream->codec->codec_id == CODEC_ID_NONE)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
226 gxf_write_timecode_auxiliary(pb, stream);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
227 else
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
228 put_le64(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
229 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
230
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
231 /* file system version */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
232 put_byte(pb, 0x4E);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
233 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
234 put_be32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
235
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
236 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
237 gxf_write_mpeg_auxiliary(pb, stream);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
238
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
239 /* frame rate */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
240 put_byte(pb, 0x50);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
241 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
242 put_be32(pb, stream->frame_rate_index);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
243
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
244 /* lines per frame */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
245 put_byte(pb, 0x51);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
246 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
247 put_be32(pb, stream->lines_index);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
248
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
249 /* fields per frame */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
250 put_byte(pb, 0x52);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
251 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
252 put_be32(pb, stream->fields);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
253
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
254 return updateSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
255 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
256
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
257 static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
258 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
259 offset_t pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
260 const char *filename = strrchr(ctx->fc->filename, '/');
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
261
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
262 pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
263 put_be16(pb, 0); /* size */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
264
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
265 /* name */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
266 if (filename)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
267 filename++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
268 else
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
269 filename = ctx->fc->filename;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
270 put_byte(pb, 0x40);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
271 put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
272 put_tag(pb, SERVER_PATH);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
273 put_tag(pb, filename);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
274 put_byte(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
275
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
276 /* first field */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
277 put_byte(pb, 0x41);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
278 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
279 put_be32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
280
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
281 /* last field */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
282 put_byte(pb, 0x42);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
283 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
284 put_be32(pb, ctx->nb_frames);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
285
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
286 /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
287 put_byte(pb, 0x43);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
288 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
289 put_be32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
290
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
291 put_byte(pb, 0x44);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
292 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
293 put_be32(pb, ctx->nb_frames);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
294
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
295 /* estimated size */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
296 put_byte(pb, 0x45);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
297 put_byte(pb, 4);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
298 put_be32(pb, url_fsize(pb) / 1024);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
299
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
300 return updateSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
301 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
302
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
303 static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
304 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
305 offset_t pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
306 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
307
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
308 pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
309 put_be16(pb, 0); /* size */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
310 for (i = 0; i < ctx->fc->nb_streams; ++i)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
311 gxf_write_track_description(pb, &ctx->streams[i]);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
312 return updateSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
313 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
314
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
315 static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
316 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
317 offset_t pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
318
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
319 gxf_write_packet_header(pb, PKT_MAP);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
320
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
321 /* preamble */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
322 put_byte(pb, 0xE0); /* version */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
323 put_byte(pb, 0xFF); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
324
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
325 gxf_write_material_data_section(pb, ctx);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
326 gxf_write_track_description_section(pb, ctx);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
327
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
328 return updatePacketSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
329 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
330
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
331 #if 0
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
332 static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
333 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
334 offset_t pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
335 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
336
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
337 gxf_write_packet_header(pb, PKT_FLT);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
338
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
339 put_le32(pb, 1000); /* number of fields */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
340 put_le32(pb, 0); /* number of active flt entries */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
341
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
342 for (i = 0; i < 1000; ++i) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
343 put_le32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
344 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
345 return updatePacketSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
346 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
347 #endif
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
348
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
349 static int gxf_write_umf_material_description(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
350 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
351 put_le32(pb, ctx->flags);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
352 put_le32(pb, ctx->nb_frames); /* length of the longest track */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
353 put_le32(pb, ctx->nb_frames); /* length of the shortest track */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
354 put_le32(pb, 0); /* mark in */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
355 put_le32(pb, ctx->nb_frames); /* mark out */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
356 put_le32(pb, 0); /* timecode mark in */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
357 put_le32(pb, ctx->nb_frames); /* timecode mark out */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
358 put_le64(pb, ctx->fc->timestamp); /* modification time */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
359 put_le64(pb, ctx->fc->timestamp); /* creation time */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
360 put_le16(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
361 put_le16(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
362 put_le16(pb, ctx->audio_tracks);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
363 put_le16(pb, 0); /* timecode track count */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
364 put_le16(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
365 put_le16(pb, ctx->mpeg_tracks);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
366 return 48;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
367 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
368
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
369 static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
370 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
371 put_le32(pb, ctx->umf_length); /* total length of the umf data */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
372 put_le32(pb, 3); /* version */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
373 put_le32(pb, ctx->fc->nb_streams);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
374 put_le32(pb, ctx->umf_track_offset); /* umf track section offset */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
375 put_le32(pb, ctx->umf_track_size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
376 put_le32(pb, ctx->fc->nb_streams);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
377 put_le32(pb, ctx->umf_media_offset);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
378 put_le32(pb, ctx->umf_media_size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
379 put_le32(pb, ctx->umf_user_data_offset); /* user data offset */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
380 put_le32(pb, ctx->umf_user_data_size); /* user data size */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
381 put_le32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
382 put_le32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
383 return 48;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
384 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
385
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
386 static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
387 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
388 offset_t pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
389 int tracks[255]={0};
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
390 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
391
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
392 ctx->umf_track_offset = pos - ctx->umf_start_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
393 for (i = 0; i < ctx->fc->nb_streams; ++i) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
394 AVStream *st = ctx->fc->streams[i];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
395 GXFStreamContext *sc = &ctx->streams[i];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
396 int id = 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
397
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
398 switch (st->codec->codec_id) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
399 case CODEC_ID_MPEG1VIDEO: id= 'L'; break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
400 case CODEC_ID_MPEG2VIDEO: id= 'M'; break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
401 case CODEC_ID_PCM_S16LE: id= 'A'; break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
402 case CODEC_ID_DVVIDEO: id= sc->track_type == 6 ? 'E' : 'D'; break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
403 case CODEC_ID_MJPEG: id= 'V'; break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
404 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
405 sc->media_info= id << 8;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
406 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
407 sc->media_info |= '0' + (tracks[id]++);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
408 put_le16(pb, sc->media_info);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
409 put_le16(pb, 1);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
410 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
411 return url_ftell(pb) - pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
412 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
413
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
414 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, GXFStreamContext *stream)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
415 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
416 if (stream->codec->pix_fmt == PIX_FMT_YUV422P)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
417 put_le32(pb, 2);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
418 else
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
419 put_le32(pb, 1); /* default to 420 */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
420 put_le32(pb, 1); /* closed = 1, open = 0, unknown = 255 */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
421 put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
422 put_le32(pb, 1); /* I picture per GOP */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
423 put_le32(pb, stream->p_per_gop);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
424 put_le32(pb, stream->b_per_gop);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
425 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
426 put_le32(pb, 2);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
427 else if (stream->codec->codec_id == CODEC_ID_MPEG1VIDEO)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
428 put_le32(pb, 1);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
429 else
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
430 put_le32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
431 put_le32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
432 return 32;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
433 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
434
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
435 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *track)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
436 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
437 /* FIXME implement */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
438 put_be32(pb, 0); /* drop frame flag */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
439 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
440 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
441 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
442 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
443 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
444 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
445 put_be32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
446 return 32;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
447 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
448
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
449 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *track)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
450 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
451 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
452
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
453 for (i = 0; i < 8; i++) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
454 put_be32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
455 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
456 return 32;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
457 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
458
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
459 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *track)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
460 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
461 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
462 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
463 put_le32(pb, 0); /* number of fields over which to ramp up sound level */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
464 put_le32(pb, 0); /* number of fields over which to ramp down sound level */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
465 put_le32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
466 put_le32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
467 return 32;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
468 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
469
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
470 #if 0
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
471 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
472 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
473 put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
474 put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
475 put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
476 put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
477 return 32;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
478 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
479 #endif
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
480
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
481 static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
482 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
483 offset_t pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
484 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
485
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
486 pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
487 ctx->umf_media_offset = pos - ctx->umf_start_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
488 for (i = 0; i < ctx->fc->nb_streams; ++i) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
489 GXFStreamContext *sc = &ctx->streams[i];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
490 char buffer[88];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
491 offset_t startpos, curpos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
492 int path_size = strlen(ES_NAME_PATTERN);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
493
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
494 startpos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
495 put_le16(pb, 0); /* length */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
496 put_le16(pb, sc->media_info);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
497 put_le16(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
498 put_le16(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
499 put_le32(pb, ctx->nb_frames);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
500 put_le32(pb, 0); /* attributes rw, ro */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
501 put_le32(pb, 0); /* mark in */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
502 put_le32(pb, ctx->nb_frames); /* mark out */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
503 strncpy(buffer, ES_NAME_PATTERN, path_size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
504 put_buffer(pb, (uint8_t *)buffer, path_size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
505 put_be16(pb, sc->media_info);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
506 put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
507 put_le32(pb, sc->track_type);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
508 put_le32(pb, sc->sample_rate);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
509 put_le32(pb, sc->sample_size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
510 put_le32(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
511 switch (sc->codec->codec_id) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
512 case CODEC_ID_MPEG2VIDEO:
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
513 gxf_write_umf_media_mpeg(pb, sc);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
514 break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
515 case CODEC_ID_PCM_S16LE:
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
516 gxf_write_umf_media_audio(pb, sc);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
517 break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
518 case CODEC_ID_DVVIDEO:
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
519 gxf_write_umf_media_dv(pb, sc);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
520 break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
521 default:
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
522 gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
523 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
524 curpos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
525 url_fseek(pb, startpos, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
526 put_le16(pb, curpos - startpos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
527 url_fseek(pb, curpos, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
528 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
529 return url_ftell(pb) - pos;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
530 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
531
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
532 static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
533 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
534 offset_t pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
535 ctx->umf_user_data_offset = pos - ctx->umf_start_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
536 put_le32(pb, 20);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
537 put_le32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
538 put_le16(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
539 put_le16(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
540 put_le32(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
541 put_byte(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
542 put_byte(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
543 put_byte(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
544 put_byte(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
545 return 20;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
546 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
547
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
548 static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
549 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
550 offset_t pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
551
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
552 gxf_write_packet_header(pb, PKT_UMF);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
553
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
554 /* preamble */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
555 put_byte(pb, 3); /* first and last (only) packet */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
556 put_be32(pb, ctx->umf_length); /* data length */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
557
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
558 ctx->umf_start_offset = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
559 gxf_write_umf_payload(pb, ctx);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
560 gxf_write_umf_material_description(pb, ctx);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
561 ctx->umf_track_size = gxf_write_umf_track_description(pb, ctx);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
562 ctx->umf_media_size = gxf_write_umf_media_description(pb, ctx);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
563 ctx->umf_user_data_size = gxf_write_umf_user_data(pb, ctx);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
564 ctx->umf_length = url_ftell(pb) - ctx->umf_start_offset;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
565 return updatePacketSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
566 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
567
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
568 static int gxf_write_header(AVFormatContext *s)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
569 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
570 ByteIOContext *pb = &s->pb;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
571 GXFContext *gxf = s->priv_data;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
572 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
573
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
574 gxf->fc = s;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
575 gxf->flags |= 0x00080000; /* material is simple clip */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
576 for (i = 0; i < s->nb_streams; ++i) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
577 AVStream *st = s->streams[i];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
578 GXFStreamContext *sc = &gxf->streams[i];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
579
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
580 sc->codec = st->codec;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
581 sc->index = i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
582 sc->media_type = codec_get_tag(gxf_media_types, sc->codec->codec_id);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
583 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
584 if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
585 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
586 return -1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
587 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
588 if (st->codec->sample_rate != 48000) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
589 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
590 return -1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
591 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
592 if (st->codec->channels != 1) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
593 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
594 return -1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
595 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
596 sc->track_type = 2;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
597 sc->sample_rate = st->codec->sample_rate;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
598 sc->sample_size = 16;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
599 sc->frame_rate_index = -2;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
600 sc->lines_index = -2;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
601 sc->fields = -2;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
602 gxf->audio_tracks++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
603 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
604 fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
605 } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
606 /* FIXME check from time_base ? */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
607 if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
608 sc->frame_rate_index = 5;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
609 sc->sample_rate = 60;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
610 gxf->flags |= 0x00000080;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
611 } else { /* assume PAL */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
612 sc->frame_rate_index = 6;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
613 sc->media_type++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
614 sc->sample_rate = 50;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
615 gxf->flags |= 0x00000040;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
616 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
617 gxf->sample_rate = sc->sample_rate;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
618 if (gxf_find_lines_index(sc) < 0)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
619 sc->lines_index = -1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
620 sc->sample_size = st->codec->bit_rate;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
621 sc->fields = 2; /* interlaced */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
622 switch (sc->codec->codec_id) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
623 case CODEC_ID_MPEG2VIDEO:
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
624 sc->track_type = 4;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
625 gxf->mpeg_tracks++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
626 gxf->flags |= 0x00008000;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
627 break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
628 case CODEC_ID_DVVIDEO:
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
629 if (sc->codec->pix_fmt == PIX_FMT_YUV422P) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
630 sc->media_type += 2;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
631 sc->track_type = 6;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
632 gxf->flags |= 0x00002000;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
633 } else {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
634 sc->track_type = 5;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
635 gxf->flags |= 0x00001000;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
636 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
637 break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
638 default:
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
639 av_log(NULL, AV_LOG_ERROR, "video codec not supported\n");
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
640 return -1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
641 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
642 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
643 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
644 gxf_write_map_packet(pb, gxf);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
645 //gxf_write_flt_packet(pb, gxf);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
646 gxf_write_umf_packet(pb, gxf);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
647 put_flush_packet(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
648 return 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
649 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
650
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
651 static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
652 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
653 offset_t pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
654
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
655 gxf_write_packet_header(pb, PKT_EOS);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
656 return updatePacketSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
657 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
658
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
659 static int gxf_write_trailer(AVFormatContext *s)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
660 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
661 ByteIOContext *pb = &s->pb;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
662 GXFContext *gxf = s->priv_data;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
663 offset_t end;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
664 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
665
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
666 for (i = 0; i < s->nb_streams; ++i) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
667 if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
668 fifo_free(&gxf->streams[i].audio_buffer);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
669 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
670 if (s->streams[i]->codec->frame_number > gxf->nb_frames)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
671 gxf->nb_frames = 2 * s->streams[i]->codec->frame_number;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
672 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
673
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
674 gxf_write_eos_packet(pb, gxf);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
675 end = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
676 url_fseek(pb, 0, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
677 /* overwrite map and umf packets with new values */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
678 gxf_write_map_packet(pb, gxf);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
679 //gxf_write_flt_packet(pb, gxf);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
680 gxf_write_umf_packet(pb, gxf);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
681 url_fseek(pb, end, SEEK_SET);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
682 return 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
683 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
684
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
685 static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
686 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
687 GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
688
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
689 put_byte(pb, sc->media_type);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
690 put_byte(pb, sc->index);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
691 put_be32(pb, ctx->field_number);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
692 if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
693 put_be16(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
694 put_be16(pb, size / 2);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
695 } else if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
696 if (pkt->flags & PKT_FLAG_KEY) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
697 put_byte(pb, 0x0d);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
698 sc->iframes++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
699 } else if (pkt->pts != pkt->dts) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
700 put_byte(pb, 0x0f);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
701 sc->bframes++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
702 } else {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
703 put_byte(pb, 0x0e);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
704 sc->pframes++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
705 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
706 put_be24(pb, size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
707 } else if (sc->codec->codec_id == CODEC_ID_DVVIDEO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
708 put_byte(pb, size / 4096);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
709 put_be24(pb, 0);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
710 } else
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
711 put_be32(pb, size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
712 put_be32(pb, ctx->field_number);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
713 put_byte(pb, 1); /* flags */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
714 put_byte(pb, 0); /* reserved */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
715 return 16;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
716 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
717
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
718 static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
719 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
720 GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
721 offset_t pos = url_ftell(pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
722 int padding = 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
723
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
724 gxf_write_packet_header(pb, PKT_MEDIA);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
725 if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
726 padding = 4 - pkt->size % 4;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
727 gxf_write_media_preamble(pb, ctx, pkt, pkt->size + padding);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
728 if (sc->codec->codec_type == CODEC_TYPE_AUDIO)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
729 put_buffer(pb, pkt->data, GXF_AUDIO_PACKET_SIZE);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
730 else {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
731 ctx->field_number += 2;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
732 put_buffer(pb, pkt->data, pkt->size);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
733 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
734 gxf_write_padding(pb, padding);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
735 return updatePacketSize(pb, pos);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
736 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
737
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
738 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
739 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
740 GXFContext *gxf = s->priv_data;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
741
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
742 gxf_write_media_packet(&s->pb, gxf, pkt);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
743 put_flush_packet(&s->pb);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
744 return 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
745 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
746
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
747 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
748 {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
749 AVPacketList *pktl, **next_point, *this_pktl;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
750 GXFContext *gxf = s->priv_data;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
751 GXFStreamContext *sc;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
752 int i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
753
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
754 if (pkt) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
755 sc = &gxf->streams[pkt->stream_index];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
756 if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
757 fifo_write(&sc->audio_buffer, pkt->data, pkt->size, NULL);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
758 } else {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
759 this_pktl = av_mallocz(sizeof(AVPacketList));
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
760 this_pktl->pkt = *pkt;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
761 if(pkt->destruct == av_destruct_packet)
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
762 pkt->destruct = NULL; // non shared -> must keep original from being freed
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
763 else
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
764 av_dup_packet(&this_pktl->pkt); //shared -> must dup
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
765 next_point = &s->packet_buffer;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
766 while(*next_point){
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
767 AVStream *st= s->streams[ pkt->stream_index];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
768 AVStream *st2= s->streams[ (*next_point)->pkt.stream_index];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
769 int64_t left= st2->time_base.num * (int64_t)st ->time_base.den;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
770 int64_t right= st ->time_base.num * (int64_t)st2->time_base.den;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
771 if((*next_point)->pkt.dts * left > pkt->dts * right) //FIXME this can overflow
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
772 break;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
773 next_point= &(*next_point)->next;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
774 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
775 this_pktl->next = *next_point;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
776 *next_point = this_pktl;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
777 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
778 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
779
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
780 if (gxf->audio_written == gxf->audio_tracks) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
781 if (!s->packet_buffer) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
782 gxf->audio_written = 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
783 return 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
784 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
785 pktl = s->packet_buffer;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
786 *out = pktl->pkt;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
787 s->packet_buffer = pktl->next;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
788 av_freep(&pktl);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
789 return 1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
790 } else {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
791 for (i = 0; i < s->nb_streams; i++) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
792 sc = &gxf->streams[i];
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
793 if (sc->codec->codec_type == CODEC_TYPE_AUDIO &&
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
794 (flush || fifo_size(&sc->audio_buffer, NULL) >= GXF_AUDIO_PACKET_SIZE)) {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
795 int size = flush ? fifo_size(&sc->audio_buffer, NULL) : GXF_AUDIO_PACKET_SIZE;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
796 av_new_packet(out, GXF_AUDIO_PACKET_SIZE);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
797 fifo_read(&sc->audio_buffer, out->data, size, NULL);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
798 gxf->audio_written++;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
799 out->stream_index = i;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
800 out->size = size;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
801 return 1;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
802 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
803 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
804 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
805 av_init_packet(out);
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
806 return 0;
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
807 }
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
808
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
809 AVOutputFormat gxf_muxer = {
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
810 "gxf",
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
811 "GXF format",
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
812 NULL,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
813 "gxf",
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
814 sizeof(GXFContext),
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
815 CODEC_ID_PCM_S16LE,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
816 CODEC_ID_MPEG2VIDEO,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
817 gxf_write_header,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
818 gxf_write_packet,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
819 gxf_write_trailer,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
820 0,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
821 NULL,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
822 gxf_interleave_packet,
5b04d98e11bb GXF muxer
bcoudurier
parents:
diff changeset
823 };