-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_GXF_HD.patch
412 lines (403 loc) · 17.6 KB
/
02_GXF_HD.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c
index aae90b3..bf556bb 100644
--- a/libavformat/gxfenc.c
+++ b/libavformat/gxfenc.c
@@ -66,6 +66,7 @@ typedef struct GXFStreamContext {
typedef struct GXFContext {
AVClass *av_class;
uint32_t nb_fields;
+ uint16_t progressive;
uint16_t audio_tracks;
uint16_t mpeg_tracks;
int64_t creation_time;
@@ -75,7 +76,7 @@ typedef struct GXFContext {
uint32_t umf_length;
uint16_t umf_track_size;
uint16_t umf_media_size;
- AVRational time_base;
+ AVRational time_base; ///< timebase is for fields not frames. Even with progressive content!
int flags;
GXFStreamContext timecode_track;
unsigned *flt_entries; ///< offsets of packets /1024, starts after 2nd video field
@@ -113,6 +114,7 @@ static const AVCodecTag gxf_media_types[] = {
{ AV_CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
{ AV_CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
{ AV_CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
+ { AV_CODEC_ID_DVVIDEO, 25 }, /* 100M DVCPRO */
{ AV_CODEC_ID_NONE, 0 },
};
@@ -283,6 +285,7 @@ static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc,
gxf_write_timecode_auxiliary(pb, gxf);
break;
case 4: /* MPEG2 */
+ case 7: /* MPE2HD */
case 9: /* MPEG1 */
gxf_write_mpeg_auxiliary(pb, s->streams[index]);
break;
@@ -448,7 +451,7 @@ static int gxf_write_umf_material_description(AVFormatContext *s)
{
GXFContext *gxf = s->priv_data;
AVIOContext *pb = s->pb;
- int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
+ int timecode_base;
int64_t timestamp = 0;
AVDictionaryEntry *t;
uint64_t nb_fields;
@@ -458,16 +461,59 @@ static int gxf_write_umf_material_description(AVFormatContext *s)
if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
timestamp = ff_iso8601_to_unix_time(t->value);
+ if (gxf->progressive) {
+ switch (gxf->time_base.den) {
+ case 120:
+ case 120000:
+ timecode_base = 60;
+ break;
+ case 100:
+ timecode_base = 50;
+ break;
+ case 60000:
+ case 60:
+ timecode_base = 30;
+ break;
+ case 50:
+ timecode_base = 25;
+ break;
+ case 48000:
+ case 48:
+ timecode_base = 24;
+ break;
+ default:
+ timecode_base = 60; //default for audio only files
+ }
+ nb_fields = (gxf->nb_fields / 2) +
+ gxf->tc.hh * (timecode_base * 3600) +
+ gxf->tc.mm * (timecode_base * 60) +
+ gxf->tc.ss * timecode_base +
+ gxf->tc.ff;
+
+ } else {
+ switch (gxf->time_base.den) {
+ case 60:
+ case 60000:
+ timecode_base = 60;
+ break;
+ case 50:
+ timecode_base = 50;
+ break;
+ default:
+ timecode_base = 60; //default for audio only files
+ }
+ nb_fields = gxf->nb_fields +
+ gxf->tc.hh * (timecode_base * 3600) +
+ gxf->tc.mm * (timecode_base * 60) +
+ gxf->tc.ss * timecode_base +
+ gxf->tc.ff;
+
+ }
+
timecode_in = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
gxf->tc.hh, gxf->tc.mm,
gxf->tc.ss, gxf->tc.ff);
- nb_fields = gxf->nb_fields +
- gxf->tc.hh * (timecode_base * 3600) +
- gxf->tc.mm * (timecode_base * 60) +
- gxf->tc.ss * timecode_base +
- gxf->tc.ff;
-
timecode_out = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
nb_fields / (timecode_base * 3600) % 24,
nb_fields / (timecode_base * 60) % 60,
@@ -700,7 +746,19 @@ static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
if (!vsc)
return;
- sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
+ switch (vsc->sample_rate) {
+ case 60:
+ case 30:
+ sc->media_type = 7;
+ break;
+ case 50:
+ case 25:
+ sc->media_type = 8;
+ break;
+ default:
+ sc->media_type = 24;
+ break;
+ }
sc->sample_rate = vsc->sample_rate;
sc->media_info = ('T'<<8) | '0';
sc->track_type = 3;
@@ -793,67 +851,205 @@ static int gxf_write_header(AVFormatContext *s)
1024*1024);
sc->aspect_ratio = (fabs(av_q2d(display_aspect_ratio)) >= 1.777);
- /* FIXME check from time_base ? */
- if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
- sc->frame_rate_index = 5;
- sc->sample_rate = 60;
- gxf->flags |= 0x00000080;
- gxf->time_base = (AVRational){ 1001, 60000 };
- } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
- sc->frame_rate_index = 6;
- sc->media_type++;
- sc->sample_rate = 50;
- gxf->flags |= 0x00000040;
- gxf->time_base = (AVRational){ 1, 50 };
- } else {
- av_log(s, AV_LOG_ERROR, "unsupported video resolution, "
- "gxf muxer only accepts PAL or NTSC resolutions currently\n");
- return -1;
+ switch (st->codec->height) {
+ case 480:
+ case 512:
+ if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
+ sc->frame_rate_index = 5;
+ sc->sample_rate = 60;
+ gxf->time_base = (AVRational){ 1001, 60000 };
+ gxf->flags |= 0x00000080;
+ sc->fields = 2;
+ gxf->progressive = 0;
+ } else {
+ av_log(s, AV_LOG_ERROR, "Invalid frame rate for NTSC content\n");
+ return -1;
+ }
+ break;
+ case 576:
+ case 608:
+ if (fabs(av_q2d(st->codec->time_base) - 1/25.0) == 0.0) {
+ sc->frame_rate_index = 6;
+ sc->sample_rate = 50;
+ gxf->time_base = (AVRational){ 1, 50 };
+ gxf->flags |= 0x00000040;
+ sc->fields = 2;
+ gxf->progressive = 0;
+ } else {
+ av_log(s, AV_LOG_ERROR, "Invalid frame rate for PAL content\n");
+ return -1;
+ }
+ break;
+ case 720:
+ if (fabs(av_q2d(st->codec->time_base) - 1/60.0) == 0.0) {
+ sc->frame_rate_index = 1;
+ sc->sample_rate = 60;
+ gxf->time_base = (AVRational){ 1, 120 };
+ gxf->flags |= 0x00000080;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1001/60000.0) < 0.0001) {
+ sc->frame_rate_index = 2;
+ sc->sample_rate = 60;
+ gxf->time_base = (AVRational){ 1001, 120000 };
+ gxf->flags |= 0x00000080;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1/50.0) == 0.0) {
+ sc->frame_rate_index = 3;
+ sc->sample_rate = 50;
+ gxf->time_base = (AVRational){ 1, 100 };
+ gxf->flags |= 0x00000040;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else {
+ av_log(s, AV_LOG_ERROR, "Invalid frame rate for 720p content\n");
+ return -1;
+ }
+ break;
+ /* Problem - ffmpeg always uses frames, but with gxf everything is fields,
+ so 1080p@30 and 1080i@60, both use "30" in ffmpeg's frame-rate argument,
+ so we can't use timebase to differentiate between 30p and 60i
+
+ For now it is assumed 1080 content is interlaced in order to
+ support HD-SDI since 1080p isn't supported until 3G-SDI
+ */
+ case 1080:
+ if (fabs(av_q2d(st->codec->time_base) - 1/30.0) == 0.0) {
+ sc->frame_rate_index = 4;
+ sc->sample_rate = 60;
+ gxf->time_base = (AVRational){ 1, 60 };
+ gxf->flags |= 0x00000080;
+ sc->fields = 2;
+ gxf->progressive = 0;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
+ sc->frame_rate_index = 5;
+ sc->sample_rate = 60;
+ gxf->time_base = (AVRational){ 1001, 60000 };
+ gxf->flags |= 0x00000080;
+ sc->fields = 2;
+ gxf->progressive = 0;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1/25.0) == 0.0) {
+ sc->frame_rate_index = 6;
+ sc->sample_rate = 50;
+ gxf->time_base = (AVRational){ 1, 50 };
+ gxf->flags |= 0x00000040;
+ sc->fields = 2;
+ gxf->progressive = 0;
+ } /* else if (fabs(av_q2d(st->codec->time_base) - 1/30.0) == 0.0) {
+ sc->frame_rate_index = 4;
+ sc->sample_rate = 30;
+ gxf->time_base = (AVRational){ 1, 60 };
+ gxf->flags |= 0x00000400;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
+ sc->frame_rate_index = 5;
+ sc->sample_rate = 30;
+ gxf->time_base = (AVRational){ 1001, 60000 };
+ gxf->flags |= 0x00000400;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) {
+ sc->frame_rate_index = 6;
+ sc->sample_rate = 25;
+ gxf->time_base = (AVRational){ 1, 50 };
+ gxf->flags |= 0x00000200;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1/24.0) == 0.0) {
+ sc->frame_rate_index = 7;
+ sc->sample_rate = 24;
+ gxf->time_base = (AVRational){ 1, 48 };
+ gxf->flags |= 0x00000100;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else if (fabs(av_q2d(st->codec->time_base) - 1001/24000.0) < 0.0001) {
+ sc->frame_rate_index = 8;
+ sc->sample_rate = 24;
+ gxf->time_base = (AVRational){ 1001, 48000 };
+ gxf->flags |= 0x00000100;
+ sc->fields = 1;
+ gxf->progressive = 1;
+ } else {
+ av_log(s, AV_LOG_ERROR, "Invalid frame rate for 1080i or 1080p content\n");
+ return -1;
+ }*/
+ break;
+ default:
+ av_log(s, AV_LOG_ERROR, "Unsupported frame size");
+ return -1;
}
+
if (!tcr)
tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
if (gxf_find_lines_index(st) < 0)
sc->lines_index = -1;
sc->sample_size = st->codec->bit_rate;
- sc->fields = 2; /* interlaced */
-
- vsc = sc;
switch (st->codec->codec_id) {
- case AV_CODEC_ID_MJPEG:
- sc->track_type = 1;
- gxf->flags |= 0x00004000;
- media_info = 'J';
- break;
- case AV_CODEC_ID_MPEG1VIDEO:
- sc->track_type = 9;
- gxf->mpeg_tracks++;
- media_info = 'L';
- break;
- case AV_CODEC_ID_MPEG2VIDEO:
- sc->first_gop_closed = -1;
- sc->track_type = 4;
- gxf->mpeg_tracks++;
- gxf->flags |= 0x00008000;
- media_info = 'M';
- break;
- case AV_CODEC_ID_DVVIDEO:
- if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P) {
- sc->media_type += 2;
- sc->track_type = 6;
- gxf->flags |= 0x00002000;
- media_info = 'E';
- } else {
- sc->track_type = 5;
- gxf->flags |= 0x00001000;
- media_info = 'D';
- }
- break;
- default:
- av_log(s, AV_LOG_ERROR, "video codec not supported\n");
- return -1;
+ case AV_CODEC_ID_MJPEG:
+ sc->track_type = 1;
+ gxf->flags |= 0x00004000;
+ media_info = 'J';
+ sc->media_type = sc->sample_rate == 50 ? 4 : 3;
+ break;
+ case AV_CODEC_ID_MPEG1VIDEO:
+ sc->track_type = 9;
+ gxf->mpeg_tracks++;
+ media_info = 'L';
+ sc->media_type = sc->sample_rate == 50 ? 23 : 22;
+ break;
+ case AV_CODEC_ID_MPEG2VIDEO:
+ if (st->codec->pix_fmt != AV_PIX_FMT_YUV422P && st->codec->pix_fmt != AV_PIX_FMT_YUV420P) {
+ av_log(s, AV_LOG_ERROR, "Unsupported pix_fmt for GXF\n");
+ return -1;
+ }
+ sc->first_gop_closed = -1;
+ gxf->mpeg_tracks++;
+ if (st->codec->height >= 720) {
+ sc->track_type = 7;
+ media_info = 'H';
+ gxf->flags |= 0x00000800;
+ sc->media_type = 20;
+ } else {
+ sc->track_type = 4;
+ media_info = 'M';
+ gxf->flags |= 0x00008000;
+ sc->media_type = sc->sample_rate == 50 ? 12 : 11;
+ }
+ break;
+ case AV_CODEC_ID_DVVIDEO:
+ if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P) {
+ if (st->codec->height >= 720) {
+ sc->media_type = 25;
+ gxf->flags |= 0x01000000;
+ media_info = 'F';
+ sc->track_type = 10;
+ } else {
+ sc->media_type = sc->sample_rate == 50 ? 16 : 15;
+ gxf->flags |= 0x00002000;
+ media_info = 'E';
+ sc->track_type = 6;
+ }
+ } else {
+ if (st->codec->height < 720) {
+ sc->track_type = 5;
+ gxf->flags |= 0x00001000;
+ media_info = 'D';
+ sc->media_type = sc->sample_rate == 50 ? 14 : 13;
+ } else {
+ av_log(s, AV_LOG_ERROR, "DV format not supported by GXF\n");
+ return -1;
+ }
+ }
+ break;
+ default:
+ av_log(s, AV_LOG_ERROR, "Video codec not supported\n");
+ return -1;
}
+ vsc = sc;
}
/* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
@@ -874,6 +1070,7 @@ static int gxf_write_header(AVFormatContext *s)
gxf_write_flt_packet(s);
gxf_write_umf_packet(s);
+
gxf->packet_count = 3;
avio_flush(pb);
@@ -993,8 +1190,11 @@ static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
int ret;
gxf_write_packet_header(pb, PKT_MEDIA);
- if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
- padding = 4 - pkt->size % 4;
+ if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) /* MPEG-2 frames must be padded */
+ if (st->codec->height >= 720 && pkt->size % 16)
+ padding = 16 - pkt->size % 16;
+ else if (st->codec->height < 720 && pkt->size % 4)
+ padding = 4 - pkt->size % 4;
else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
gxf_write_media_preamble(s, pkt, pkt->size + padding);