00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avassert.h"
00023 #include "libavutil/intfloat.h"
00024 #include "libavutil/opt.h"
00025 #include "libavutil/mathematics.h"
00026 #include "libavutil/timecode.h"
00027 #include "avformat.h"
00028 #include "internal.h"
00029 #include "gxf.h"
00030 #include "riff.h"
00031 #include "audiointerleave.h"
00032
00033 #define GXF_AUDIO_PACKET_SIZE 65536
00034
00035 #define GXF_TIMECODE(c, d, h, m, s, f) \
00036 ((c) << 30 | (d) << 29 | (h) << 24 | (m) << 16 | (s) << 8 | (f))
00037
00038 typedef struct GXFTimecode{
00039 int hh;
00040 int mm;
00041 int ss;
00042 int ff;
00043 int color;
00044 int drop;
00045 } GXFTimecode;
00046
00047 typedef struct GXFStreamContext {
00048 AudioInterleaveContext aic;
00049 uint32_t track_type;
00050 uint32_t sample_size;
00051 uint32_t sample_rate;
00052 uint16_t media_type;
00053 uint16_t media_info;
00054 int frame_rate_index;
00055 int lines_index;
00056 int fields;
00057 int iframes;
00058 int pframes;
00059 int bframes;
00060 int p_per_gop;
00061 int b_per_i_or_p;
00062 int first_gop_closed;
00063 unsigned order;
00064 } GXFStreamContext;
00065
00066 typedef struct GXFContext {
00067 AVClass *av_class;
00068 uint32_t nb_fields;
00069 uint16_t audio_tracks;
00070 uint16_t mpeg_tracks;
00071 int64_t creation_time;
00072 uint32_t umf_start_offset;
00073 uint32_t umf_track_offset;
00074 uint32_t umf_media_offset;
00075 uint32_t umf_length;
00076 uint16_t umf_track_size;
00077 uint16_t umf_media_size;
00078 AVRational time_base;
00079 int flags;
00080 GXFStreamContext timecode_track;
00081 unsigned *flt_entries;
00082 unsigned flt_entries_nb;
00083 uint64_t *map_offsets;
00084 unsigned map_offsets_nb;
00085 unsigned packet_count;
00086 GXFTimecode tc;
00087 } GXFContext;
00088
00089 static const struct {
00090 int height, index;
00091 } gxf_lines_tab[] = {
00092 { 480, 1 },
00093 { 512, 1 },
00094 { 576, 2 },
00095 { 608, 2 },
00096 { 1080, 4 },
00097 { 720, 6 },
00098 };
00099
00100 static const AVCodecTag gxf_media_types[] = {
00101 { AV_CODEC_ID_MJPEG , 3 },
00102 { AV_CODEC_ID_MJPEG , 4 },
00103 { AV_CODEC_ID_PCM_S24LE , 9 },
00104 { AV_CODEC_ID_PCM_S16LE , 10 },
00105 { AV_CODEC_ID_MPEG2VIDEO, 11 },
00106 { AV_CODEC_ID_MPEG2VIDEO, 12 },
00107 { AV_CODEC_ID_DVVIDEO , 13 },
00108 { AV_CODEC_ID_DVVIDEO , 14 },
00109 { AV_CODEC_ID_DVVIDEO , 15 },
00110 { AV_CODEC_ID_DVVIDEO , 16 },
00111 { AV_CODEC_ID_AC3 , 17 },
00112
00113 { AV_CODEC_ID_MPEG2VIDEO, 20 },
00114 { AV_CODEC_ID_MPEG1VIDEO, 22 },
00115 { AV_CODEC_ID_MPEG1VIDEO, 23 },
00116 { AV_CODEC_ID_NONE, 0 },
00117 };
00118
00119 #define SERVER_PATH "EXT:/PDR/default/"
00120 #define ES_NAME_PATTERN "EXT:/PDR/default/ES."
00121
00122 static int gxf_find_lines_index(AVStream *st)
00123 {
00124 GXFStreamContext *sc = st->priv_data;
00125 int i;
00126
00127 for (i = 0; i < 6; ++i) {
00128 if (st->codec->height == gxf_lines_tab[i].height) {
00129 sc->lines_index = gxf_lines_tab[i].index;
00130 return 0;
00131 }
00132 }
00133 return -1;
00134 }
00135
00136 static void gxf_write_padding(AVIOContext *pb, int64_t to_pad)
00137 {
00138 for (; to_pad > 0; to_pad--) {
00139 avio_w8(pb, 0);
00140 }
00141 }
00142
00143 static int64_t updatePacketSize(AVIOContext *pb, int64_t pos)
00144 {
00145 int64_t curpos;
00146 int size;
00147
00148 size = avio_tell(pb) - pos;
00149 if (size % 4) {
00150 gxf_write_padding(pb, 4 - size % 4);
00151 size = avio_tell(pb) - pos;
00152 }
00153 curpos = avio_tell(pb);
00154 avio_seek(pb, pos + 6, SEEK_SET);
00155 avio_wb32(pb, size);
00156 avio_seek(pb, curpos, SEEK_SET);
00157 return curpos - pos;
00158 }
00159
00160 static int64_t updateSize(AVIOContext *pb, int64_t pos)
00161 {
00162 int64_t curpos;
00163
00164 curpos = avio_tell(pb);
00165 avio_seek(pb, pos, SEEK_SET);
00166 avio_wb16(pb, curpos - pos - 2);
00167 avio_seek(pb, curpos, SEEK_SET);
00168 return curpos - pos;
00169 }
00170
00171 static void gxf_write_packet_header(AVIOContext *pb, GXFPktType type)
00172 {
00173 avio_wb32(pb, 0);
00174 avio_w8(pb, 1);
00175 avio_w8(pb, type);
00176 avio_wb32(pb, 0);
00177 avio_wb32(pb, 0);
00178 avio_w8(pb, 0xE1);
00179 avio_w8(pb, 0xE2);
00180 }
00181
00182 static int gxf_write_mpeg_auxiliary(AVIOContext *pb, AVStream *st)
00183 {
00184 GXFStreamContext *sc = st->priv_data;
00185 char buffer[1024];
00186 int size, starting_line;
00187
00188 if (sc->iframes) {
00189 sc->p_per_gop = sc->pframes / sc->iframes;
00190 if (sc->pframes % sc->iframes)
00191 sc->p_per_gop++;
00192 if (sc->pframes) {
00193 sc->b_per_i_or_p = sc->bframes / sc->pframes;
00194 if (sc->bframes % sc->pframes)
00195 sc->b_per_i_or_p++;
00196 }
00197 if (sc->p_per_gop > 9)
00198 sc->p_per_gop = 9;
00199 if (sc->b_per_i_or_p > 9)
00200 sc->b_per_i_or_p = 9;
00201 }
00202 if (st->codec->height == 512 || st->codec->height == 608)
00203 starting_line = 7;
00204 else if (st->codec->height == 480)
00205 starting_line = 20;
00206 else
00207 starting_line = 23;
00208
00209 size = snprintf(buffer, sizeof(buffer), "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
00210 "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
00211 (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
00212 st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
00213 starting_line, (st->codec->height + 15) / 16);
00214 av_assert0(size < sizeof(buffer));
00215 avio_w8(pb, TRACK_MPG_AUX);
00216 avio_w8(pb, size + 1);
00217 avio_write(pb, (uint8_t *)buffer, size + 1);
00218 return size + 3;
00219 }
00220
00221 static int gxf_write_timecode_auxiliary(AVIOContext *pb, GXFContext *gxf)
00222 {
00223 uint32_t timecode = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
00224 gxf->tc.hh, gxf->tc.mm,
00225 gxf->tc.ss, gxf->tc.ff);
00226
00227 avio_wl32(pb, timecode);
00228
00229 avio_wl32(pb, 0);
00230 return 8;
00231 }
00232
00233 static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
00234 {
00235 GXFContext *gxf = s->priv_data;
00236 AVIOContext *pb = s->pb;
00237 int64_t pos;
00238 int mpeg = sc->track_type == 4 || sc->track_type == 9;
00239
00240
00241 avio_w8(pb, sc->media_type + 0x80);
00242 avio_w8(pb, index + 0xC0);
00243
00244 pos = avio_tell(pb);
00245 avio_wb16(pb, 0);
00246
00247
00248 avio_w8(pb, TRACK_NAME);
00249 avio_w8(pb, strlen(ES_NAME_PATTERN) + 3);
00250 avio_write(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN) - 1);
00251 avio_wb16(pb, sc->media_info);
00252 avio_w8(pb, 0);
00253
00254 if (!mpeg) {
00255
00256 avio_w8(pb, TRACK_AUX);
00257 avio_w8(pb, 8);
00258 if (sc->track_type == 3)
00259 gxf_write_timecode_auxiliary(pb, gxf);
00260 else
00261 avio_wl64(pb, 0);
00262 }
00263
00264
00265 avio_w8(pb, TRACK_VER);
00266 avio_w8(pb, 4);
00267 avio_wb32(pb, 0);
00268
00269 if (mpeg)
00270 gxf_write_mpeg_auxiliary(pb, s->streams[index]);
00271
00272
00273 avio_w8(pb, TRACK_FPS);
00274 avio_w8(pb, 4);
00275 avio_wb32(pb, sc->frame_rate_index);
00276
00277
00278 avio_w8(pb, TRACK_LINES);
00279 avio_w8(pb, 4);
00280 avio_wb32(pb, sc->lines_index);
00281
00282
00283 avio_w8(pb, TRACK_FPF);
00284 avio_w8(pb, 4);
00285 avio_wb32(pb, sc->fields);
00286
00287 return updateSize(pb, pos);
00288 }
00289
00290 static int gxf_write_material_data_section(AVFormatContext *s)
00291 {
00292 GXFContext *gxf = s->priv_data;
00293 AVIOContext *pb = s->pb;
00294 int64_t pos;
00295 int len;
00296 const char *filename = strrchr(s->filename, '/');
00297
00298 pos = avio_tell(pb);
00299 avio_wb16(pb, 0);
00300
00301
00302 if (filename)
00303 filename++;
00304 else
00305 filename = s->filename;
00306 len = strlen(filename);
00307
00308 avio_w8(pb, MAT_NAME);
00309 avio_w8(pb, strlen(SERVER_PATH) + len + 1);
00310 avio_write(pb, SERVER_PATH, sizeof(SERVER_PATH) - 1);
00311 avio_write(pb, filename, len);
00312 avio_w8(pb, 0);
00313
00314
00315 avio_w8(pb, MAT_FIRST_FIELD);
00316 avio_w8(pb, 4);
00317 avio_wb32(pb, 0);
00318
00319
00320 avio_w8(pb, MAT_LAST_FIELD);
00321 avio_w8(pb, 4);
00322 avio_wb32(pb, gxf->nb_fields);
00323
00324
00325 avio_w8(pb, MAT_MARK_IN);
00326 avio_w8(pb, 4);
00327 avio_wb32(pb, 0);
00328
00329 avio_w8(pb, MAT_MARK_OUT);
00330 avio_w8(pb, 4);
00331 avio_wb32(pb, gxf->nb_fields);
00332
00333
00334 avio_w8(pb, MAT_SIZE);
00335 avio_w8(pb, 4);
00336 avio_wb32(pb, avio_size(pb) / 1024);
00337
00338 return updateSize(pb, pos);
00339 }
00340
00341 static int gxf_write_track_description_section(AVFormatContext *s)
00342 {
00343 GXFContext *gxf = s->priv_data;
00344 AVIOContext *pb = s->pb;
00345 int64_t pos;
00346 int i;
00347
00348 pos = avio_tell(pb);
00349 avio_wb16(pb, 0);
00350 for (i = 0; i < s->nb_streams; ++i)
00351 gxf_write_track_description(s, s->streams[i]->priv_data, i);
00352
00353 gxf_write_track_description(s, &gxf->timecode_track, s->nb_streams);
00354
00355 return updateSize(pb, pos);
00356 }
00357
00358 static int gxf_write_map_packet(AVFormatContext *s, int rewrite)
00359 {
00360 GXFContext *gxf = s->priv_data;
00361 AVIOContext *pb = s->pb;
00362 int64_t pos = avio_tell(pb);
00363
00364 if (!rewrite) {
00365 if (!(gxf->map_offsets_nb % 30)) {
00366 gxf->map_offsets = av_realloc_f(gxf->map_offsets,
00367 sizeof(*gxf->map_offsets),
00368 gxf->map_offsets_nb+30);
00369 if (!gxf->map_offsets) {
00370 av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n");
00371 return -1;
00372 }
00373 }
00374 gxf->map_offsets[gxf->map_offsets_nb++] = pos;
00375 }
00376
00377 gxf_write_packet_header(pb, PKT_MAP);
00378
00379
00380 avio_w8(pb, 0xE0);
00381 avio_w8(pb, 0xFF);
00382
00383 gxf_write_material_data_section(s);
00384 gxf_write_track_description_section(s);
00385
00386 return updatePacketSize(pb, pos);
00387 }
00388
00389 static int gxf_write_flt_packet(AVFormatContext *s)
00390 {
00391 GXFContext *gxf = s->priv_data;
00392 AVIOContext *pb = s->pb;
00393 int64_t pos = avio_tell(pb);
00394 int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
00395 int flt_entries = gxf->nb_fields / fields_per_flt;
00396 int i = 0;
00397
00398 gxf_write_packet_header(pb, PKT_FLT);
00399
00400 avio_wl32(pb, fields_per_flt);
00401 avio_wl32(pb, flt_entries);
00402
00403 if (gxf->flt_entries) {
00404 for (i = 0; i < flt_entries; i++)
00405 avio_wl32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
00406 }
00407
00408 for (; i < 1000; i++)
00409 avio_wl32(pb, 0);
00410
00411 return updatePacketSize(pb, pos);
00412 }
00413
00414 static int gxf_write_umf_material_description(AVFormatContext *s)
00415 {
00416 GXFContext *gxf = s->priv_data;
00417 AVIOContext *pb = s->pb;
00418 int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
00419 int64_t timestamp = 0;
00420 AVDictionaryEntry *t;
00421 uint64_t nb_fields;
00422 uint32_t timecode_in;
00423 uint32_t timecode_out;
00424
00425 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
00426 timestamp = ff_iso8601_to_unix_time(t->value);
00427
00428 timecode_in = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
00429 gxf->tc.hh, gxf->tc.mm,
00430 gxf->tc.ss, gxf->tc.ff);
00431
00432 nb_fields = gxf->nb_fields +
00433 gxf->tc.hh * (timecode_base * 3600) +
00434 gxf->tc.mm * (timecode_base * 60) +
00435 gxf->tc.ss * timecode_base +
00436 gxf->tc.ff;
00437
00438 timecode_out = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
00439 nb_fields / (timecode_base * 3600) % 24,
00440 nb_fields / (timecode_base * 60) % 60,
00441 nb_fields / timecode_base % 60,
00442 nb_fields % timecode_base);
00443
00444 avio_wl32(pb, gxf->flags);
00445 avio_wl32(pb, gxf->nb_fields);
00446 avio_wl32(pb, gxf->nb_fields);
00447 avio_wl32(pb, 0);
00448 avio_wl32(pb, gxf->nb_fields);
00449 avio_wl32(pb, timecode_in);
00450 avio_wl32(pb, timecode_out);
00451 avio_wl64(pb, timestamp);
00452 avio_wl64(pb, timestamp);
00453 avio_wl16(pb, 0);
00454 avio_wl16(pb, 0);
00455 avio_wl16(pb, gxf->audio_tracks);
00456 avio_wl16(pb, 1);
00457 avio_wl16(pb, 0);
00458 avio_wl16(pb, gxf->mpeg_tracks);
00459 return 48;
00460 }
00461
00462 static int gxf_write_umf_payload(AVFormatContext *s)
00463 {
00464 GXFContext *gxf = s->priv_data;
00465 AVIOContext *pb = s->pb;
00466
00467 avio_wl32(pb, gxf->umf_length);
00468 avio_wl32(pb, 3);
00469 avio_wl32(pb, s->nb_streams+1);
00470 avio_wl32(pb, gxf->umf_track_offset);
00471 avio_wl32(pb, gxf->umf_track_size);
00472 avio_wl32(pb, s->nb_streams+1);
00473 avio_wl32(pb, gxf->umf_media_offset);
00474 avio_wl32(pb, gxf->umf_media_size);
00475 avio_wl32(pb, gxf->umf_length);
00476 avio_wl32(pb, 0);
00477 avio_wl32(pb, 0);
00478 avio_wl32(pb, 0);
00479 return 48;
00480 }
00481
00482 static int gxf_write_umf_track_description(AVFormatContext *s)
00483 {
00484 AVIOContext *pb = s->pb;
00485 GXFContext *gxf = s->priv_data;
00486 int64_t pos = avio_tell(pb);
00487 int i;
00488
00489 gxf->umf_track_offset = pos - gxf->umf_start_offset;
00490 for (i = 0; i < s->nb_streams; ++i) {
00491 GXFStreamContext *sc = s->streams[i]->priv_data;
00492 avio_wl16(pb, sc->media_info);
00493 avio_wl16(pb, 1);
00494 }
00495
00496 avio_wl16(pb, gxf->timecode_track.media_info);
00497 avio_wl16(pb, 1);
00498
00499 return avio_tell(pb) - pos;
00500 }
00501
00502 static int gxf_write_umf_media_mpeg(AVIOContext *pb, AVStream *st)
00503 {
00504 GXFStreamContext *sc = st->priv_data;
00505
00506 if (st->codec->pix_fmt == PIX_FMT_YUV422P)
00507 avio_wl32(pb, 2);
00508 else
00509 avio_wl32(pb, 1);
00510 avio_wl32(pb, sc->first_gop_closed == 1);
00511 avio_wl32(pb, 3);
00512 avio_wl32(pb, 1);
00513 avio_wl32(pb, sc->p_per_gop);
00514 avio_wl32(pb, sc->b_per_i_or_p);
00515 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
00516 avio_wl32(pb, 2);
00517 else if (st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO)
00518 avio_wl32(pb, 1);
00519 else
00520 avio_wl32(pb, 0);
00521 avio_wl32(pb, 0);
00522 return 32;
00523 }
00524
00525 static int gxf_write_umf_media_timecode(AVIOContext *pb, int drop)
00526 {
00527 avio_wl32(pb, drop);
00528 avio_wl32(pb, 0);
00529 avio_wl32(pb, 0);
00530 avio_wl32(pb, 0);
00531 avio_wl32(pb, 0);
00532 avio_wl32(pb, 0);
00533 avio_wl32(pb, 0);
00534 avio_wl32(pb, 0);
00535 return 32;
00536 }
00537
00538 static int gxf_write_umf_media_dv(AVIOContext *pb, GXFStreamContext *sc)
00539 {
00540 int i;
00541
00542 for (i = 0; i < 8; i++) {
00543 avio_wb32(pb, 0);
00544 }
00545 return 32;
00546 }
00547
00548 static int gxf_write_umf_media_audio(AVIOContext *pb, GXFStreamContext *sc)
00549 {
00550 avio_wl64(pb, av_double2int(1));
00551 avio_wl64(pb, av_double2int(1));
00552 avio_wl32(pb, 0);
00553 avio_wl32(pb, 0);
00554 avio_wl32(pb, 0);
00555 avio_wl32(pb, 0);
00556 return 32;
00557 }
00558
00559 static int gxf_write_umf_media_description(AVFormatContext *s)
00560 {
00561 GXFContext *gxf = s->priv_data;
00562 AVIOContext *pb = s->pb;
00563 int64_t pos;
00564 int i, j;
00565
00566 pos = avio_tell(pb);
00567 gxf->umf_media_offset = pos - gxf->umf_start_offset;
00568 for (i = 0; i <= s->nb_streams; ++i) {
00569 GXFStreamContext *sc;
00570 int64_t startpos, curpos;
00571
00572 if (i == s->nb_streams)
00573 sc = &gxf->timecode_track;
00574 else
00575 sc = s->streams[i]->priv_data;
00576
00577 startpos = avio_tell(pb);
00578 avio_wl16(pb, 0);
00579 avio_wl16(pb, sc->media_info);
00580 avio_wl16(pb, 0);
00581 avio_wl16(pb, 0);
00582 avio_wl32(pb, gxf->nb_fields);
00583 avio_wl32(pb, 0);
00584 avio_wl32(pb, 0);
00585 avio_wl32(pb, gxf->nb_fields);
00586 avio_write(pb, ES_NAME_PATTERN, strlen(ES_NAME_PATTERN));
00587 avio_wb16(pb, sc->media_info);
00588 for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
00589 avio_w8(pb, 0);
00590 avio_wl32(pb, sc->track_type);
00591 avio_wl32(pb, sc->sample_rate);
00592 avio_wl32(pb, sc->sample_size);
00593 avio_wl32(pb, 0);
00594
00595 if (sc == &gxf->timecode_track)
00596 gxf_write_umf_media_timecode(pb, gxf->tc.drop);
00597 else {
00598 AVStream *st = s->streams[i];
00599 switch (st->codec->codec_id) {
00600 case AV_CODEC_ID_MPEG1VIDEO:
00601 case AV_CODEC_ID_MPEG2VIDEO:
00602 gxf_write_umf_media_mpeg(pb, st);
00603 break;
00604 case AV_CODEC_ID_PCM_S16LE:
00605 gxf_write_umf_media_audio(pb, sc);
00606 break;
00607 case AV_CODEC_ID_DVVIDEO:
00608 gxf_write_umf_media_dv(pb, sc);
00609 break;
00610 }
00611 }
00612
00613 curpos = avio_tell(pb);
00614 avio_seek(pb, startpos, SEEK_SET);
00615 avio_wl16(pb, curpos - startpos);
00616 avio_seek(pb, curpos, SEEK_SET);
00617 }
00618 return avio_tell(pb) - pos;
00619 }
00620
00621 static int gxf_write_umf_packet(AVFormatContext *s)
00622 {
00623 GXFContext *gxf = s->priv_data;
00624 AVIOContext *pb = s->pb;
00625 int64_t pos = avio_tell(pb);
00626
00627 gxf_write_packet_header(pb, PKT_UMF);
00628
00629
00630 avio_w8(pb, 3);
00631 avio_wb32(pb, gxf->umf_length);
00632
00633 gxf->umf_start_offset = avio_tell(pb);
00634 gxf_write_umf_payload(s);
00635 gxf_write_umf_material_description(s);
00636 gxf->umf_track_size = gxf_write_umf_track_description(s);
00637 gxf->umf_media_size = gxf_write_umf_media_description(s);
00638 gxf->umf_length = avio_tell(pb) - gxf->umf_start_offset;
00639 return updatePacketSize(pb, pos);
00640 }
00641
00642 static const int GXF_samples_per_frame[] = { 32768, 0 };
00643
00644 static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
00645 {
00646 if (!vsc)
00647 return;
00648
00649 sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
00650 sc->sample_rate = vsc->sample_rate;
00651 sc->media_info = ('T'<<8) | '0';
00652 sc->track_type = 3;
00653 sc->frame_rate_index = vsc->frame_rate_index;
00654 sc->lines_index = vsc->lines_index;
00655 sc->sample_size = 16;
00656 sc->fields = vsc->fields;
00657 }
00658
00659 static int gxf_init_timecode(AVFormatContext *s, GXFTimecode *tc, const char *tcstr, int fields)
00660 {
00661 char c;
00662
00663 if (sscanf(tcstr, "%d:%d:%d%c%d", &tc->hh, &tc->mm, &tc->ss, &c, &tc->ff) != 5) {
00664 av_log(s, AV_LOG_ERROR, "unable to parse timecode, "
00665 "syntax: hh:mm:ss[:;.]ff\n");
00666 return -1;
00667 }
00668
00669 tc->color = 0;
00670 tc->drop = c != ':';
00671
00672 if (fields == 2)
00673 tc->ff = tc->ff * 2;
00674
00675 return 0;
00676 }
00677
00678 static int gxf_write_header(AVFormatContext *s)
00679 {
00680 AVIOContext *pb = s->pb;
00681 GXFContext *gxf = s->priv_data;
00682 GXFStreamContext *vsc = NULL;
00683 uint8_t tracks[255] = {0};
00684 int i, media_info = 0;
00685 AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
00686
00687 if (!pb->seekable) {
00688 av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome\n");
00689 return -1;
00690 }
00691
00692 gxf->flags |= 0x00080000;
00693 for (i = 0; i < s->nb_streams; ++i) {
00694 AVStream *st = s->streams[i];
00695 GXFStreamContext *sc = av_mallocz(sizeof(*sc));
00696 if (!sc)
00697 return AVERROR(ENOMEM);
00698 st->priv_data = sc;
00699
00700 sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
00701 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00702 if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE) {
00703 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
00704 return -1;
00705 }
00706 if (st->codec->sample_rate != 48000) {
00707 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
00708 return -1;
00709 }
00710 if (st->codec->channels != 1) {
00711 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
00712 return -1;
00713 }
00714 sc->track_type = 2;
00715 sc->sample_rate = st->codec->sample_rate;
00716 avpriv_set_pts_info(st, 64, 1, sc->sample_rate);
00717 sc->sample_size = 16;
00718 sc->frame_rate_index = -2;
00719 sc->lines_index = -2;
00720 sc->fields = -2;
00721 gxf->audio_tracks++;
00722 gxf->flags |= 0x04000000;
00723 media_info = 'A';
00724 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00725 if (i != 0) {
00726 av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
00727 return -1;
00728 }
00729
00730 if (st->codec->height == 480 || st->codec->height == 512) {
00731 sc->frame_rate_index = 5;
00732 sc->sample_rate = 60;
00733 gxf->flags |= 0x00000080;
00734 gxf->time_base = (AVRational){ 1001, 60000 };
00735 } else if (st->codec->height == 576 || st->codec->height == 608) {
00736 sc->frame_rate_index = 6;
00737 sc->media_type++;
00738 sc->sample_rate = 50;
00739 gxf->flags |= 0x00000040;
00740 gxf->time_base = (AVRational){ 1, 50 };
00741 } else {
00742 av_log(s, AV_LOG_ERROR, "unsupported video resolution, "
00743 "gxf muxer only accepts PAL or NTSC resolutions currently\n");
00744 return -1;
00745 }
00746 if (!tcr)
00747 tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
00748 avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
00749 if (gxf_find_lines_index(st) < 0)
00750 sc->lines_index = -1;
00751 sc->sample_size = st->codec->bit_rate;
00752 sc->fields = 2;
00753
00754 vsc = sc;
00755
00756 switch (st->codec->codec_id) {
00757 case AV_CODEC_ID_MJPEG:
00758 sc->track_type = 1;
00759 gxf->flags |= 0x00004000;
00760 media_info = 'J';
00761 break;
00762 case AV_CODEC_ID_MPEG1VIDEO:
00763 sc->track_type = 9;
00764 gxf->mpeg_tracks++;
00765 media_info = 'L';
00766 break;
00767 case AV_CODEC_ID_MPEG2VIDEO:
00768 sc->first_gop_closed = -1;
00769 sc->track_type = 4;
00770 gxf->mpeg_tracks++;
00771 gxf->flags |= 0x00008000;
00772 media_info = 'M';
00773 break;
00774 case AV_CODEC_ID_DVVIDEO:
00775 if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
00776 sc->media_type += 2;
00777 sc->track_type = 6;
00778 gxf->flags |= 0x00002000;
00779 media_info = 'E';
00780 } else {
00781 sc->track_type = 5;
00782 gxf->flags |= 0x00001000;
00783 media_info = 'D';
00784 }
00785 break;
00786 default:
00787 av_log(s, AV_LOG_ERROR, "video codec not supported\n");
00788 return -1;
00789 }
00790 }
00791
00792 sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
00793 sc->order = s->nb_streams - st->index;
00794 }
00795
00796 if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
00797 return -1;
00798
00799 if (tcr)
00800 gxf_init_timecode(s, &gxf->tc, tcr->value, vsc->fields);
00801
00802 gxf_init_timecode_track(&gxf->timecode_track, vsc);
00803 gxf->flags |= 0x200000;
00804
00805 gxf_write_map_packet(s, 0);
00806 gxf_write_flt_packet(s);
00807 gxf_write_umf_packet(s);
00808
00809 gxf->packet_count = 3;
00810
00811 avio_flush(pb);
00812 return 0;
00813 }
00814
00815 static int gxf_write_eos_packet(AVIOContext *pb)
00816 {
00817 int64_t pos = avio_tell(pb);
00818
00819 gxf_write_packet_header(pb, PKT_EOS);
00820 return updatePacketSize(pb, pos);
00821 }
00822
00823 static int gxf_write_trailer(AVFormatContext *s)
00824 {
00825 GXFContext *gxf = s->priv_data;
00826 AVIOContext *pb = s->pb;
00827 int64_t end;
00828 int i;
00829
00830 ff_audio_interleave_close(s);
00831
00832 gxf_write_eos_packet(pb);
00833 end = avio_tell(pb);
00834 avio_seek(pb, 0, SEEK_SET);
00835
00836 gxf_write_map_packet(s, 1);
00837 gxf_write_flt_packet(s);
00838 gxf_write_umf_packet(s);
00839 avio_flush(pb);
00840
00841 for (i = 1; i < gxf->map_offsets_nb; i++) {
00842 avio_seek(pb, gxf->map_offsets[i], SEEK_SET);
00843 gxf_write_map_packet(s, 1);
00844 avio_flush(pb);
00845 }
00846
00847 avio_seek(pb, end, SEEK_SET);
00848
00849 av_freep(&gxf->flt_entries);
00850 av_freep(&gxf->map_offsets);
00851
00852 return 0;
00853 }
00854
00855 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
00856 {
00857 uint32_t c=-1;
00858 int i;
00859 for(i=0; i<size-4 && c!=0x100; i++){
00860 c = (c<<8) + buf[i];
00861 if(c == 0x1B8 && sc->first_gop_closed == -1)
00862 sc->first_gop_closed= (buf[i+4]>>6)&1;
00863 }
00864 return (buf[i+1]>>3)&7;
00865 }
00866
00867 static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
00868 {
00869 GXFContext *gxf = s->priv_data;
00870 AVIOContext *pb = s->pb;
00871 AVStream *st = s->streams[pkt->stream_index];
00872 GXFStreamContext *sc = st->priv_data;
00873 unsigned field_nb;
00874
00875
00876
00877 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00878 field_nb = gxf->nb_fields;
00879 } else {
00880 field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
00881 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
00882 }
00883
00884 avio_w8(pb, sc->media_type);
00885 avio_w8(pb, st->index);
00886 avio_wb32(pb, field_nb);
00887 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00888 avio_wb16(pb, 0);
00889 avio_wb16(pb, size / 2);
00890 } else if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
00891 int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
00892 if (frame_type == AV_PICTURE_TYPE_I) {
00893 avio_w8(pb, 0x0d);
00894 sc->iframes++;
00895 } else if (frame_type == AV_PICTURE_TYPE_B) {
00896 avio_w8(pb, 0x0f);
00897 sc->bframes++;
00898 } else {
00899 avio_w8(pb, 0x0e);
00900 sc->pframes++;
00901 }
00902 avio_wb24(pb, size);
00903 } else if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) {
00904 avio_w8(pb, size / 4096);
00905 avio_wb24(pb, 0);
00906 } else
00907 avio_wb32(pb, size);
00908 avio_wb32(pb, field_nb);
00909 avio_w8(pb, 1);
00910 avio_w8(pb, 0);
00911 return 16;
00912 }
00913
00914 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
00915 {
00916 GXFContext *gxf = s->priv_data;
00917 AVIOContext *pb = s->pb;
00918 AVStream *st = s->streams[pkt->stream_index];
00919 int64_t pos = avio_tell(pb);
00920 int padding = 0;
00921 int packet_start_offset = avio_tell(pb) / 1024;
00922
00923 gxf_write_packet_header(pb, PKT_MEDIA);
00924 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO && pkt->size % 4)
00925 padding = 4 - pkt->size % 4;
00926 else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
00927 padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
00928 gxf_write_media_preamble(s, pkt, pkt->size + padding);
00929 avio_write(pb, pkt->data, pkt->size);
00930 gxf_write_padding(pb, padding);
00931
00932 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00933 if (!(gxf->flt_entries_nb % 500)) {
00934 gxf->flt_entries = av_realloc_f(gxf->flt_entries,
00935 sizeof(*gxf->flt_entries),
00936 gxf->flt_entries_nb+500);
00937 if (!gxf->flt_entries) {
00938 av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
00939 return -1;
00940 }
00941 }
00942 gxf->flt_entries[gxf->flt_entries_nb++] = packet_start_offset;
00943 gxf->nb_fields += 2;
00944 }
00945
00946 updatePacketSize(pb, pos);
00947
00948 gxf->packet_count++;
00949 if (gxf->packet_count == 100) {
00950 gxf_write_map_packet(s, 0);
00951 gxf->packet_count = 0;
00952 }
00953
00954 avio_flush(pb);
00955
00956 return 0;
00957 }
00958
00959 static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
00960 {
00961 GXFContext *gxf = s->priv_data;
00962 AVPacket *pkt[2] = { cur, next };
00963 int i, field_nb[2];
00964 GXFStreamContext *sc[2];
00965
00966 for (i = 0; i < 2; i++) {
00967 AVStream *st = s->streams[pkt[i]->stream_index];
00968 sc[i] = st->priv_data;
00969 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00970 field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
00971 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
00972 field_nb[i] &= ~1;
00973 } else
00974 field_nb[i] = pkt[i]->dts;
00975 }
00976
00977 return field_nb[1] > field_nb[0] ||
00978 (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
00979 }
00980
00981 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
00982 {
00983 if (pkt && s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
00984 pkt->duration = 2;
00985 return ff_audio_rechunk_interleave(s, out, pkt, flush,
00986 ff_interleave_packet_per_dts, gxf_compare_field_nb);
00987 }
00988
00989 AVOutputFormat ff_gxf_muxer = {
00990 .name = "gxf",
00991 .long_name = NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"),
00992 .extensions = "gxf",
00993 .priv_data_size = sizeof(GXFContext),
00994 .audio_codec = AV_CODEC_ID_PCM_S16LE,
00995 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
00996 .write_header = gxf_write_header,
00997 .write_packet = gxf_write_packet,
00998 .write_trailer = gxf_write_trailer,
00999 .interleave_packet = gxf_interleave_packet,
01000 };